Can you add inline base64 encoded image to a Mandr

2019-06-28 08:31发布

问题:

Does anyone know if and how-to add a Base64 encoded image to a Mandrill Template.

I use a Mandrill template to make sure all my emails have the same look and feel and I have a simple and small logo which I'd prefer to add to my template following these recommendations: http://help.mandrill.com/entries/25252978-Tips-for-using-images-in-Mandrill-emails

However i'm unsure on where and how to add the neccesary code in my HTML template. Anyone have an idea if it's possible and how-to do it?

Thx

回答1:

I have probably found the answer to my own questions.

NO - Mandrill doesn't allow you to add an inline image or to upload the image attachment as code to the template.

SOLUTION - you need to add the array [images] to the $message as suggested in the API.

Here is an PHP example function to do it:

// Add cid image to $message
function add_email_logo($message) {
  $logo = array (
    'images' => array (
      array (
              'type' => 'image/png',
              'name' => 'logo',
              'content' => 'iVBORw0KGgoAAAANSUhEUgAA....etc.'
      )
    )
  );

  return array_merge( $message, $logo);
}

and use the image src="cid:{name}"

<img src="cid:logo"/>



回答2:

In the middle of the page you link to it tells you that you can use the img tag with base64 encoded images

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">

The src attribute is using a Data URI scheme. The general format is

<img src="data:(MIME TYPE);base64,(IMAGEDATA BASE 64 ENCODED)">

Heres the list of image media types

image/gif
image/jpeg
image/pjpeg
image/png
image/svg+xml
image/vnd.djvy
image/example

The esiest way to get the base64 encoded image already in html tag form is to use a converter online. Googling base64 encode image yeild plenty of results that have this capability.



回答3:

In Mandril example

<img src="cid:image_name">

Should be replaced with

<img src="cid:image_name" />

Else, image will be just attached to mail, not shown inside