Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mandrill: how to attach image as inline and as attachement?

Tags:

php

mandrill

I am trying to attach an image which is in base64 format like

data:image/jpeg;base64,/9j....

And I have following attachment code in php:

$attachment_encoded5 = $record['upload_file'];
                //$attachment_encoded5 = base64_encode($record['upload_file']);
                $attachment_array[] = 
                    array(
                        'type' => 'image/jpeg',
                        'name' => $record['name'].".jpeg",
                        'content' => $attachment_encoded5
                    );

So now what I am doing wrong here: - Do I need to convert dataurl image into base64? - Do I need to use any other code to attach an image? - Finally, how I can also attach an image as inline image?

Thanks in advance:) Please let me know as I am bit stuck:(

like image 731
Jimil Avatar asked Dec 06 '25 16:12

Jimil


1 Answers

Include images as inline attachments with your API calls using the images parameter. You'll need to provide the image name (Content-ID), the content (as a base64 encoded string), and the image MIME type. Reference the image name in the 'src' in your HTML content:

<img src="cid:image_name" />

READ MORE

so you need to wrap your existing array into another i.e images

function addInlineImage(){

   $image_name=$record['name'].".jpeg";

   $attachment=array(

    'images'=>

      array(
        'type' => 'image/jpeg',
        'name' => $image_name,
        'content' => $attachment_encoded5
      )

   );

  return [$attachment,$image_name];
}

You can return the image name and attachment from the function and use

list($attachment,$image_name)=addInlineImage();

and then use it in the tag as below

<img src="cid:<?=$image_name?>" />

just make sure the attachment you provide in the function addInlineImages() is base64encoded

For most SMTP libraries, including inline images is handled automatically. For example, if you insert an image inline, an img tag is added which then references the Content-ID of the image that's attached. How to add inline images will depend on the SMTP library being used.

like image 144
Muhammad Omer Aslam Avatar answered Dec 08 '25 05:12

Muhammad Omer Aslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!