Embed image email library


#1

Enonic version: Enonic XP 6.14.1
OS: Windows 10

Hello,

How could I embed an image in the body of an email using the mail library? I have attached an image but I cannot find the way to embed it.

Thanks a lot in advance.


#2

To embed an image in an e-mail, you need to send the e-mail as HTML. The first step is the easiest: Set the parameter contentType to be ‘text/html; charset=“UTF-8”’ or something similar, so that the e-mail is not interpreted as “text/plain”.

The second step is the complicated one: You need to format the body as HTML, keeping in mind that coding HTML for e-mail clients has quite different requirements than coding HTML for browsers.

It’s been quite a few years since I last made a proper HTML e-mail template, but back then we needed to use HTML 3.2 using tables for layouts and all that crap. Thankfully, I think e-mail clients today are probably a lot more compatible with some modern HTML than say just 5 years ago.

As far as the actual embedding of the image: Normally with HTML for e-mail you’d serve the image somewhere and use an image URL, but it might be possible to embed something like a base64-encoded string of the image data, on the format of something like this: <img src="data:image/png;base64,<BASE_64_ENCODED_STRING">. But to confirm if this is possible, you’ll need to find a resource about coding HTML for e-mail and see what is possible to do today without sacrificing too much compatability with different e-mail clients.

Good luck!

EDIT: a quick web search revealed that you can also try referencing an image attached to the e-mail by using a Content ID (a.k.a CID) in the image tag, for instance <img src="cid:the-image-cid" alt="logo" />
I’m guessing that this is one of the most common ways for e-mail clients to add images to the signature, since they often show as attachments in e-mail clients that don’t support viewing such images or that have disabled reading e-mails as HTML.


#3

Thanks for your help. I tried already referencing the attached image but it didn’t work. I will try the other method.