base64 encoded images in email signatures
Asked Answered
O

3

127

I have to include some images (company logo's etc) in email signatures. I've had all sorts of issues using the embedded images produced by the email system in question (they get sent as attachments generally) and as linked images (requiring permission to display them in the email received).

I have just seen some email from exchange that has a base64 image representation of the logo and uses a tag to do the displaying. I'm looking for some information on how I could do this in an email signature if possible (how do I generate the base64 version of the logo for a start and what code do I need to get it to work)?

I've tried simple things such as

<body>
<span>
<img src=.... >
</span>
</body>

but all I get is the alt text so I'm obviously doing something wrong here.

Overwork answered 2/2, 2012 at 9:44 Comment(1)
possible duplicate of How to embed images in emailAdmonish
O
82

2024 Update

Important! My answer below shows how to embed images using data URIs. This is a powerful feature in any modern web browser, but is still not supported in some email clients, notably Gmail.


Base-64 data is legal in an img tag and I believe your question is how to properly insert such an image tag.

You can use an online tool or a few lines of code to generate the base 64 string.

The syntax to source the image from inline data is:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

http://en.wikipedia.org/wiki/Data_URI_scheme

Odor answered 2/2, 2012 at 9:49 Comment(8)
Tried this but Gmail doesn't seem to support it. The embedded attachment solution seems to work though.Cologne
Hi Tim it would be a great help if you can provide some inputs on #11125040. Thanks in advance. Another thing is there any resolution so that mail domain like yahoo,gmail also support the your solutionFebruary
I think attachments (such as @Shadow2531's solution) may be the more widely supported solution, but the only way to know is to test several major email clients with different security settings applied. Further complicating the matter is that you can receive email both on the web and in a variety of clients. For example, Gmail may behave differently when viewed on the web versus inside of Outlook or Thunderbird.Odor
Check this link for support for using this technique in mail clients campaignmonitor.com/blog/post/3927/… (tl;dr some support but inconsistent, particularly in Outlook)Ichabod
Is embedding still unreliable for most email clients in 2016?Stockbreeder
@EirikBirkeland This website gives an overview of HTML/CSS support in email clients: caniemail.com/features/image-base64 Update 2021: Gmail doesn't support base64 images :-(Leupold
If you using nodemailer, check the solution hereEyeopening
This solution works great (including Gmail as the recipient) and avoids all traceability.Estriol
A
165

The image should be embedded in the message as an attachment like this:

--boundary
Content-Type: image/png; name="sig.png"
Content-Disposition: inline; filename="sig.png"
Content-Transfer-Encoding: base64
Content-ID: <0123456789>
Content-Location: sig.png

base64 data

--boundary

And, the HTML part would reference the image like this:

<img src="cid:0123456789">

In some clients, src="sig.png" will work too.

You'd basically have a multipart/mixed, multipart/alternative, multipart/related message where the image attachment is in the related part.

Clients shouldn't block this image either as it isn't remote.

Or, here's a multipart/alternative, multipart/related example as an mbox file (save as windows newline format and put a blank line at the end. And, use no extension or the .mbs extension):

From 
From: [email protected]
To: [email protected]
Subject: HTML Messages with Embedded Pic in Signature
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="alternative_boundary"

This is a message with multiple parts in MIME format.

--alternative_boundary
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

test

-- 
[Picture of a Christmas Tree]

--alternative_boundary
Content-Type: multipart/related; boundary="related_boundary"

--related_boundary
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <p>test</p>
        <p class="sig">-- <br><img src="cid:0123456789"></p>
    </body>
</html>

--related_boundary
Content-Type: image/png; name="sig.png"
Content-Disposition: inline; filename="sig.png"
Content-Location: sig.png
Content-ID: <0123456789>
Content-Transfer-Encoding: base64

R0lGODlhKAA8AIMLAAD//wAhAABKAABrAACUAAC1AADeAAD/AGsAAP8zM///AP//
///M//////+ZAMwAACH/C05FVFNDQVBFMi4wAwGgDwAh+QQJFAALACwAAAAAKAA8
AAME+3DJSWt1Nuu9Mf+g5IzK6IXopaxn6orlKy/jMc6vQRy4GySABK+HAiaIoQdg
uUSCBAKAYTBwbgyGA2AgsGqo0wMh7K0YEuj0sUxRoAfqB1vycBN21Ki8vOofBndR
c1AKgH8ETE1lBgo7O2JaU2UFAgRoDGoA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Q9I5KwaynaalUS
RnpBpYH4YiXoTipgIlIFtLSUFKwSBb/NtGCnb2Zl51fHo8hnhRZbSfCEKkgZkkcw
TgBgyVdxeQNRMNNMoMBOpBxFUSx+ObgYPgS1BBRss/jxxzwAqsbLRfwh1VJyF5WI
2AkIAIAAAiiUKMGMICDRXQIn6IiCW4Qs4NYZTByppBkbRAAAIf4ZQm95J3MgSGFw
cHkgSG9saWRheXMgUGFnZQA7

--related_boundary--

--alternative_boundary--

You can import that into Sylpheed or Thunderbird (with the Import/Export tools extension) or Opera's built-in mail client. Then, in Opera for example, you can toggle "prefer plain text" to see the difference between the HTML and text version. Anyway, you'll see the HTML version makes use of the embedded pic in the sig.

Arum answered 2/2, 2012 at 9:49 Comment(22)
it would be a great help if you can provide some inputs on #11125040.February
@MSach Will take a look when I get a chance.Arum
Why do you use the Content-Location header to store the filename? It's supposed to be a URI that functions as an ID so that you can reference that body part from a different body part. See RFC 2557. Since you are not using the way the RFC intends, I'm curious to know why you include it.Admonish
@Admonish It's storing a relative URI to the file, not the filename string. Opera's built-in mail client often uses it so I show it in addition to the content-id way as when supported, it's easier than messing with ids.Arum
"The image should be embedded in the message as an attachment like this ..." could You tell me how can I add this parameters in Intent?Danikadanila
@Danikadanila Sorry. Don't even know what "Intent" is.Arum
Can you tell me @Arum where the multipart/related code will go? is it supposed to be in the same HTML file?Gauntlett
@FaisalAshfaq I'm not sure what you mean. There is no HTML file. This is an eml/mbox file. The multipart/related sections are marked in the example. The HTML markup/page is one of the related sections and the image attachment is the other that the HTML part references. The related parts are inside the second alternative part.Arum
"like this"... like what? I'm with Faisal, how do you get this into your message?Ziegfeld
@Scott This is not something you do manually. Your mail client does this when you insert an image into an HTML message (via an insert command or drag and drop) that you're composing. Also, when you just attach an image file (content-disposition: attachment), your client still embeds it in the source. The only reason you'd do this manually is if you're programming a client or some server-side script or extension etc. Or, in the case of the example mbox file above, I just hand-coded it with a text editor so the example can be imported into a mail client.Arum
This question is specifically about signatures, so you're recommending his users drag and drop the image to the email every time they compose a new message?Ziegfeld
@Scott No. Any client that supports HTML signatures in HTML messages should do things automatically when you set a signature for the account/alias/identity/address once. As in, when you compose/reply, the signature with the image is automatically inserted. If the client doesn't support that, then of course, there's really nothing you can do except to insert an image every single time. Templates (or editing a sent message that looks like a template), in supported clients can help with this too.Arum
How do you get the image into the signature? Your post starts out "the image should be embedded into the message as an attachment". But now you're saying it's automatically done when you set a signature for the account. I'm not trying to be a jerk, I'm genuinely confused!Ziegfeld
@Scott That depends on the client. In Thunderbird, you choose to attach a signature from an HTML file. Then, in the HTML file, you use an HTML img tag with a src="path to local image file". It is then up to Thunderbird to base64-embed that local image and reference it when the signature is inserted.Arum
Okay, that comment jarred my understanding. What I've done is create an HTM file then loaded it into outlook. I then used the signature editors "replace image" functionality to select a local file (instead of the base64 encoded image I had manually included). This seems to do what you mean. Thanks for sticking with me.Ziegfeld
@Scott Awesome. To test that it's truly working, send a message to yourself. In the received message, view source and make sure that Outlook embedded it properly and didn't just reference the local image on your hard drive. If Outlook just does the latter, when you send to someone else, they won't see the image.Arum
hi@Arum The OP was asking about adding an image to an HTML email signature whereas this answer implies you have to add the above code in the body of an email? Or am I understanding this incorrectly? And if I am understanding this incorrectly, where do you add that --boundary code you show? Does it go in the <head> section of a .htm file or above all of the HTML in an .htm file?Barmen
@TheChewy, my post is about how an email client (or a developer coding an email source generator on a server) should generate the source of the message to embed an image in a signature. It's not about how you as a user would insert html markup in the html signature field in your client's signature options.Arum
@Shadow2531Ah OK. Thanks for clarifying.Barmen
I've a base64, how can I know the Content-ID?Instructor
@heisenberg, there's a discussion about that at <#39577886>.Arum
When do this method, does this work with tracking pixels?Caitiff
O
82

2024 Update

Important! My answer below shows how to embed images using data URIs. This is a powerful feature in any modern web browser, but is still not supported in some email clients, notably Gmail.


Base-64 data is legal in an img tag and I believe your question is how to properly insert such an image tag.

You can use an online tool or a few lines of code to generate the base 64 string.

The syntax to source the image from inline data is:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

http://en.wikipedia.org/wiki/Data_URI_scheme

Odor answered 2/2, 2012 at 9:49 Comment(8)
Tried this but Gmail doesn't seem to support it. The embedded attachment solution seems to work though.Cologne
Hi Tim it would be a great help if you can provide some inputs on #11125040. Thanks in advance. Another thing is there any resolution so that mail domain like yahoo,gmail also support the your solutionFebruary
I think attachments (such as @Shadow2531's solution) may be the more widely supported solution, but the only way to know is to test several major email clients with different security settings applied. Further complicating the matter is that you can receive email both on the web and in a variety of clients. For example, Gmail may behave differently when viewed on the web versus inside of Outlook or Thunderbird.Odor
Check this link for support for using this technique in mail clients campaignmonitor.com/blog/post/3927/… (tl;dr some support but inconsistent, particularly in Outlook)Ichabod
Is embedding still unreliable for most email clients in 2016?Stockbreeder
@EirikBirkeland This website gives an overview of HTML/CSS support in email clients: caniemail.com/features/image-base64 Update 2021: Gmail doesn't support base64 images :-(Leupold
If you using nodemailer, check the solution hereEyeopening
This solution works great (including Gmail as the recipient) and avoids all traceability.Estriol
D
4

Recently I had the same problem to include QR image/png in email. The QR image is a byte array which is generated using ZXing. We do not want to save it to a file because saving/reading from a file is too expensive (slow). So both of the answers above do not work for me. Here's what I did to solve this problem:

import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.mail.ImageHtmlEmail;
...
ImageHtmlEmail email = new ImageHtmlEmail();
byte[] qrImageBytes = createQRCode(); // get your image byte array
ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
String contentId = email.embed(qrImageDataSource, "QR Image");

Let's say the contentId is "111122223333", then your HTML part should have this:

<img src="cid: 111122223333">

There's no need to convert the byte array to Base64 because Commons Mail does the conversion for you automatically. Hope this helps.

Delorsedelos answered 23/3, 2015 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.