Signatures, Embedded Images, and all that…

I have been spending the last few weeks dealing with Crossware, our email signature platform. It’s good (don’t worry, there’s a but coming up…) in that I am enjoying dealing with the C# code and thinking how to organize signatures. Crossware uses C# to create something called formulae (which are parts of your signtuare – First Name, Last Name, and so on – and you use C# code such as if/ else clauses, arrays, dictionaries to manage and display these). The formulae are part of blocks, and blocks are part of the eventual signature templates.

This project has been fun in that I enjoy coding and organizing and thinking about things, but the platform is very v1.0 in terms of the UI and features, and signatures are in general a pain to deal with. So the fun has been tempered with that.

Anyways, last week was spent on figuring out logos and icons in emails. Crossware lets you upload images to itself and then embed these into the formulae. I learnt that such embedded is via adding the image as Base64 to an email and then referring to it via its Content-Id. To give an example here’s how the code for an email I send looks like:

There’s a lot of HTML table stuff in there you can ignore. The salient points are 1) the entire message is made up of multiple related messages; 2) the overall message has a Content-Type: multipart/related which is what tells email clients these various parts are related; 3) and in terms of embedded images, my img link has a source set to this weird “cid::44729497-5e96-4dbb-a49a-fb4540f4dba3” text.

What is that? Turns out that is a Content Id URL. It refers to a Content-Id block within this email which contains the actual image. And 4) we can see that further below as Content-ID: <44729497-5e96-4dbb-a49a-fb4540f4dba3>. The Base64 content specified by that block is the image.

Email clients thus know where to pick the embedded picture from and show the reader.

We were receiving complaints that some users are not seeing these images. While troubleshooting with my Outlook for Mac, I noticed that I too am not seeing the images when sending emails from this client (OWA and Outlook for Windows were fine). Digging into the HTML code for that I found that that’s because Outlook for Mac 1) creates a broken cid: link, pointing not to the Content-Id but to something else (in the example above the link it created was src="cid:ceFZZuO9AkuTTFevRGTg122CWImageMobile14pxpng.png" – you can find that text in the Content-Name or Content-Description of the image) and 2) worse, it does to add the Base64 image as a part of the email. So not only was the link wrong, had it been correct there was no image to pick up anyways! Hence the broken behaviour for some email clients.

Solution is to use hyperlinks instead for images. But I also wanted to explore embedding images manually.

Before I move on though, the above structure is why 1) some clients show an attachment icon in the email but don’t actually show an attachment (because, the image is actually present as a separate attachment to the image, just that it is classified as related to the main message) and 2) some clients may ignore the fact that the attachment is related and thus show it both in-line to the message as well as attachments to the email (this would also happen if the wrong multipart type is used).

A disadvantage of embedding images is that since we are converting the image to Base64 that increases the size of the image by about 33%. So it is only recommended for smaller images. Plus older browsers like IE8 have limitations with it. In fact, other versions of IE and also older versions of Edge have size limitations with it.

Apart from the Content-Id way of embedding, one can also convert the image to Base64 oneself and insert it as the source of an img link as a data URI. Like thus:

Converting an image to Base64 is easy. Here’s some PowerShell to do it:

For completeness, converting Base64 to an image (or any file) is easy too:

Again, I ran into an edge case with Outlook for Mac. If I don’t specify a size in the img tag it puts one of its own. So I should ideally do the following:

Not the end of the world, but this means for each picture I should also note the size and add it.

Surprisingly, this actually ended up working! Outlook for Mac still had some gotchas in that when I reply to an email with embedded images it wouldn’t treat the image background as transparent and instead show it as black. We finally went with linked images, but I spent all this time learning embedded images so I figured I must blog it here for posterity. :)