Jimil
Jimil

Reputation: 650

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

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:(

Upvotes: 3

Views: 1704

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23768

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.

Upvotes: 2

Related Questions