Cid in images not working while sending mail with inline images

1.7k Views Asked by At

I have a mail body with an inline image set as following:

<img src="cid:<<content-id>>">

I am using nodemailer to send mails. I also have a relative URL to the image. But the image is not being displayed in Outlook. Seems like cid is not working with recent versions of Outlook.

Other options?

Is it possible to get base64 of the image in nodejs. I have seen example with canvas and xmlhttprequest but that can't be done in node without using external modules which I don't want to.

Any solutions please?

1

There are 1 best solutions below

0
On

Using an example, if your html property is:

<p><img src="cid:c001" /></p>

then attachments property has to be as follows:

[ { path: "data:image/gif;base64,...image data here....",
     cid: "c001"
} ]

For additional embedded images, just add them to the attachments array.

The above will generate a mail as follows:

Content-Type: multipart/alternative; boundary="s1"

--s1
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

    ...the (plain)`text` part of your message
--s1
Content-Type: multipart/related; type="text/html"; boundary="s2"

--s2
Content-Type: text/html

<p><img src="cid:c001" /></p>
--s2

Content-Type: image/gif; name=attachment-1.gif
Content-ID: <c001>
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=attachment-1.gif

...image data here....
--s2
--s1