How to Embed Images in HTML Email: CID, Base64, Hosted
Nov 20268 min read
How to embed images in HTML email with a CID attachment, a base64 data URI or a hosted link, and why the mail our own site sends uses CID.
- HTML email
- Email templates
- Inline images
There are three ways to embed images in HTML email. Attach the image to the message and point to it with a cid: link, write it into the HTML as a base64 data URI, or host it on a web server and link to it. CID shows even where remote images are blocked. Hosted keeps the message small.
We had to pick one for this site. Three public routes on mkdirhq.com send email: the contact form, the free audit and trial form, and a short note to both founders when someone types their website into the audit field. All three share one template, and the template has one image in it: our logo, in a black header bar.
We chose CID, and the setup we ship is at the end, down to the byte count of the logo.
What are the ways to embed an image in an HTML email?
An email is a MIME message: a set of parts, each with its own type. That structure decides where the picture's bytes travel. With a hosted image they stay on a server and the mail client fetches them when the reader opens the message. With base64 they are pasted into the HTML text itself. With CID they ride along as a separate part of the same message, and the HTML points at that part by name.
| Method | Where the bytes live | Shows when remote images are blocked | Message size | Main way it breaks |
|---|---|---|---|---|
| Hosted link | A web server, fetched on open | No, the reader sees a blank box until they allow images | Smallest: only the URL is sent | Image blocking, and a broken image if the file ever moves |
| Base64 data URI | Inside the HTML, as text | Yes, where the client renders data URIs at all | About a third larger than the image file | Some webmail clients strip the src attribute, and large images push the HTML toward clipping limits |
| CID inline attachment | A separate part of the same message | Yes, the picture is already in the message | About a third larger than the image file, outside the HTML | Some clients also list the image as an attachment |
None of the three wins everywhere. The right one depends on how many images you send, how big they are, and whether a missing image costs you anything.
Why do hosted images not show in some emails?
Because many mail clients refuse to fetch anything from the internet until the reader says yes. Microsoft's support page for Outlook says it plainly: Outlook is configured by default to block automatic picture downloads from the internet. The reason is privacy. A remote image is a request to someone's server, and that request tells the sender the message was opened, from roughly where, and when. That is how open tracking works in most marketing tools.
For a newsletter with twelve product photos, that trade is usually fine. The reader clicks once to show images and the message stays light. For a transactional email with a single logo, a blocked image means the very first thing the reader sees is an empty grey box where the brand should be.
Hosted images also depend on the file staying put. Rename the image on the server a year from now and every old email in every inbox shows a broken picture. We did not want a logo URL we could never change.
Can you use base64 images in HTML email?
You can, and in many clients it works. The src attribute holds the whole file as text: data:image/png;base64, followed by the encoded bytes. Nothing is fetched, so nothing is blocked.
Two costs come with it. Base64 writes every 3 bytes as 4 characters, so the image grows by about a third. And those characters sit inside the HTML, which some clients cap. Gmail is widely reported to clip a message once its HTML passes roughly 102KB, showing a "View entire message" link at the cut. Google does not publish the figure, so treat it as an observed limit, not a promise.
Support is the other problem. The Can I email support table for base64 images lists several webmail services, Orange, LaPoste.net and SFR among them, that rename the src attribute so the image never loads. If your readers are mostly in one country, that table is worth reading before you commit.
For one small logo, base64 is not a disaster. We just could not find a case where it did better than CID, which carries the same bytes outside the HTML.
How do CID embedded images work?
The image goes into the message as its own MIME part with a Content-ID header. The HTML part and the image part are wrapped together in a multipart/related container, the type RFC 2387 defines for parts that belong together. In the HTML, the img tag's src is cid: followed by that same ID. RFC 2392 defines the cid: scheme: it refers to a specific body part of the same message.
Most mail libraries build this structure for you. You pass the image as an attachment with a cid field, and you write src="cid:the-same-value" in the HTML. The library writes the headers and the MIME boundaries.
The image still grows by a third, because MIME also sends binary parts as base64. The difference is where those bytes sit. They are a separate part, outside the HTML, so they do not count toward an HTML size cap.
The known downside: some clients show a CID image twice, once in the body and once as an attachment chip below it. It is cosmetic, but readers notice. Keeping the image small and giving it a plain filename makes it look less like something to open.
What we ship on mkdirhq.com
The logo is one PNG, 3,860 bytes. As base64 it is 5,148 characters. It is drawn at 214 by 72 pixels and shown at 107 by 36, so it stays sharp on dense phone screens.
Nobody exports that file from a design tool. A small script draws it from the same vector paths that render the logo on the website, so the email header can never drift from the site. The same script also writes the 512 pixel square mark that search engines read from the site's structured data. It writes the email logo out as a TypeScript file holding the base64 string and the two dimensions, and that file is committed.
The script is not part of the build. We run it by hand when the logo changes. That matters on our hosting. The image renderer it uses is the same one we took out of the server bundle when the site had to fit a size limit. Our post on cutting the bundle by a third tells that story. A string constant costs the server bundle about 5KB. The renderer would cost far more.
Keeping the bytes in code has a second benefit. The mail routes never read a file when a form is submitted, so there is no path to get wrong and no file to leave out of a deploy.
Each of the three mail routes imports one shared attachment object: the base64 string, a filename of mkdir.png, and a Content-ID of mkdir-logo. The template's header cell holds an img tag whose src is cid:mkdir-logo, with the width and height set as attributes.
| Detail in the template | Why it is there |
|---|---|
| Explicit width and height attributes on the image | The layout holds its shape before the image loads, and clients that ignore CSS still size it |
| Logo drawn on its own near-black background, never a transparent one | A client that recolours the message for dark mode cannot leave the logo invisible on its own background |
| Header colour set with both a bgcolor attribute and an inline style | Older clients read the attribute, newer ones read the style |
| Alt text of mkdir on the logo | If the image is stripped, the brand name is still there as text |
| Layout built from tables with inline styles only | Most mail clients drop style sheets and web fonts |
| A hidden preview line at the top of the body | It sets the snippet shown beside the subject in the inbox list |
| Every submitted value escaped before it goes into the HTML | A form visitor cannot inject markup into an email we send |
The escaping row is the one people skip. These emails contain text that strangers typed into a public form. Our post on contact form spam protection covers the rest of what those routes check before anything is sent.
Which method should you use for your emails?
Pick by what you are sending.
| What you send | Method we would use | Why |
|---|---|---|
| Receipts, form confirmations, password resets with a logo | CID | One small image that should show the moment the message opens |
| Newsletters and campaigns with many or large images | Hosted | Size stays down, and images can be fixed after sending |
| Email signatures | Hosted, or plain text with no image | A CID logo on every reply can pile up as attachment chips in long threads |
| Anything where you need open tracking | Hosted | Tracking needs a request to a server, and only a hosted image makes one |
| A quick internal alert or a one-off test | Base64 is fine | Simplest to write, and the readers are known |
Cold outreach is a separate case. A first email to a stranger usually does best as plain text with no images at all, because it should look like a note from a person. Images and heavy HTML in cold email are a deliverability question, and we cover who should own that in our post on email deliverability.
Whichever method you pick, send a test to the inboxes your readers use before you ship, and open it once with images blocked. A message that reads fine with every picture missing will survive any of the three.
If you do not write code, ask whoever builds your emails what your receipts and form replies look like with images turned off.
Sources
- The template, the three mail routes, the logo script and the byte counts: this site's own repository, November 2026
- The cid: URL scheme refers to a specific body part of a message: RFC 2392, Content-ID and Message-ID Uniform Resource Locators (rfc-editor.org/rfc/rfc2392)
- The multipart/related content type: RFC 2387, The MIME Multipart/Related Content-type (rfc-editor.org/rfc/rfc2387)
- Base64 encodes each 3 bytes as 4 characters: RFC 4648, The Base16, Base32, and Base64 Data Encodings (rfc-editor.org/rfc/rfc4648)
- Outlook blocks automatic picture downloads from the internet by default: Microsoft Support, Block or unblock automatic picture downloads in email messages (support.microsoft.com)
- Base64 image support by mail client, including the webmail services that strip the src attribute: Can I email, Base 64 image format (caniemail.com/features/image-base64)
- Gmail clipping long messages at about 102KB of HTML, an observed limit Google does not document: Email on Acid, Gmail email clipping (emailonacid.com/blog/article/email-development/gmail-email-clipping)