Day 5 · HTML email, images, code & security

HTML email and images: how to build emails and embed images properly

lesson 15 / 1919 min1 labpublished
Course contents
On this page
  1. Why email HTML is different
  2. Building a message that renders everywhere
  3. Images: the four ways

HTML email looks like web development and isn’t. There’s no standard rendering engine: every mail client renders HTML its own way, strips what it considers dangerous, and some of the most widely used ones are years behind browsers. Images bring their own questions: should they be attached, embedded, or loaded from a server? Will they be blocked? Will they make the message look like spam? This lesson answers all of it, and in the lab you build and send a real HTML email with an embedded logo and a hosted image.

Why email HTML is different

ClientRenderingWhat it means for you
Outlook for Windows (classic)Microsoft Word’s HTML engine, since Outlook 2007Weakest support: tables for layout, limited CSS, no max-width on most elements, no CSS background images, needs width attributes and special VML for rounded buttons and backgrounds
New Outlook for Windows, Outlook on the web, Outlook.comBrowser-basedModern, but Microsoft rewrites some CSS and applies its own dark mode
Gmail (web and apps)Browser, with heavy sanitizingSupports <style> in the head (since 2016) but drops it entirely if it finds an error, no external CSS, rewrites class names, clips messages whose HTML exceeds about 102 KB
Apple Mail (macOS, iOS)WebKitThe best support: media queries, dark mode, web fonts, even SVG
Yahoo, AOL, mobile apps, corporate webmailBrowser, sanitizedSomewhere in between; test

The resource every email developer uses is caniemail.com, which shows, feature by feature, which clients support what. Paid services such as Litmus and Email on Acid render your message in dozens of real clients.

Building a message that renders everywhere

  • Layout with tables. Nested <table role="presentation"> elements are still the only layout method that works in classic Outlook. role="presentation" tells screen readers the table is layout, not data.
  • A 600–640 px content column, centered in a full-width outer table, with width="600" as an attribute (for Outlook) and style="max-width:600px; width:100%" (for everything else, so it shrinks on phones).
  • Inline CSS. Put styles in style="" attributes. Write your CSS normally and run it through an inliner at build time (most email frameworks do it for you). Keep a <style> block only for media queries and dark mode, which can’t be inlined.
  • Web-safe fonts (Arial, Helvetica, Georgia, Verdana, system UI stacks), with web fonts only as a progressive enhancement for clients that support them.
  • Bulletproof buttons: a link styled with padding inside a table cell with a background color, not an image of a button, so the button still works when images are blocked.
  • Outlook-only code goes in conditional comments: <!--[if mso]> ... <![endif]-->. Every other client ignores it.
  • Preheader text: the first text in the body is shown as a preview after the subject in most inboxes. Put a deliberate summary there (it can be hidden visually), or the preview will be “View this email in your browser”.
  • Size: keep the HTML well under 102 KB or Gmail clips it behind a “View entire message” link, which also hides your unsubscribe link and the bottom of the message.
  • A plain-text part (multipart/alternative), written by a person, not generated from the HTML by stripping tags.

Frameworks save you from writing this by hand: MJML (a markup language that compiles to bulletproof HTML), Maizzle (Tailwind CSS for email), and React Email produce tested table layouts, inlined CSS and Outlook fallbacks.

Accessibility

Email is read by people with screen readers, on small screens, in bright sunlight. Set <html lang="...">, use real headings and paragraphs inside the cells, keep body text at 14–16 px or larger with strong contrast, write meaningful link text (“See the winter collection”, not “click here”), give every image an alt attribute (empty, alt="", for decorative ones), and never put important text only inside an image.

Dark mode

Most clients now have a dark mode, and they handle email in three ways: no change (some clients), partial color inversion of light backgrounds and dark text (Outlook.com, the Gmail app on Android), or full inversion (the Gmail app on iOS, some Outlook versions). Apple Mail respects @media (prefers-color-scheme: dark), so you can supply your own dark styles there. Practical rules: test in dark mode, avoid text baked into images (it can’t be inverted), give dark logos on transparent backgrounds a light outline or a solid background so they don’t disappear on dark gray, and don’t fight the clients that force inversion.

Images: the four ways

There are exactly four ways to get an image into an email, and they behave very differently.

Hosted (linked)Embedded (CID)Data URI (base64 in src)Plain attachment
How<img src="https://img.example.com/logo.png">Image in a multipart/related part with a Content-ID, HTML uses src="cid:..."<img src="data:image/png;base64,iVBOR...">A normal attachment
Message sizeTiny: only the URLGrows by the image size + 33%Grows by the image size + 33%, inside the HTML (counts toward Gmail’s 102 KB clip)Grows by the image + 33%
Shown in GmailYes, through Google’s image proxyYesNo: Gmail doesn’t display data URIsAs an attachment
Shown in classic OutlookOnly after the user allows downloads for external imagesYes, without askingNoAs an attachment
Shown in Apple MailYesYesYesInline or as an attachment
Can be changed or removed after sendingYes (and can break if you delete it)NoNoNo
Lets the sender detect opensYes (unreliably, see below)NoNoNo
Typical useNewsletters, marketing, anything with several imagesLogos in transactional mail and signatures, mail for internal networksAvoidDocuments the recipient should save

The practical rule: hosted images for newsletters and marketing, CID for a small logo in transactional or internal mail where you want it to show without a download prompt, never data URIs, and attachments only for files the recipient needs as files.

Hosting images properly

  • Serve them over HTTPS from your own domain or a subdomain such as img.example.com (link domains carry reputation; a shared free image host may be on a domain blocklist).
  • Keep URLs stable forever. Old emails in people’s mailboxes still load images from them; deleting the files breaks every message you ever sent.
  • No authentication, no hotlink protection, no redirects, the correct Content-Type. Gmail fetches through its proxy (googleusercontent.com), so rules that block unknown referrers or user agents break images in Gmail.
  • Use a CDN for large lists: when a newsletter goes out, hundreds of thousands of fetches arrive within minutes.

Formats and sizes

  • JPEG for photos, PNG for logos, icons and anything with transparency, GIF for simple animations. Older desktop versions of Outlook for Windows show only the first frame of an animated GIF, so put the essential message in it.
  • SVG works in Apple Mail but not in Gmail or Outlook: don’t use it for important images. WebP and AVIF support is growing but incomplete; serve JPEG, PNG or GIF unless you know your audience’s clients.
  • Retina: export at twice the display size and set the display size in HTML: a 176×104 px file with width="88" height="52". Outlook ignores CSS widths, so always set the width attribute; add style="width:100%; height:auto" for images that should shrink on phones.
  • Add style="display:block; border:0" to images in table cells, or some clients add a gap below them.
  • Compress everything. A whole message’s images under 1 MB is a reasonable ceiling; individual hero images under 200 KB load fast on mobile networks.
  • Background images need CSS background-image plus a VML fallback for classic Outlook (“bulletproof backgrounds”); frameworks generate it for you. Always set a background color too, for when the image doesn’t load.

Design for images off

Plenty of people never see your images: classic Outlook blocks external images by default until the sender is trusted, corporate policies block them, and some people turn them off. So:

  • The message must make sense with no images at all: real text for headlines and calls to action, buttons made of HTML, not pictures.
  • Every image has an alt text that says what it shows (“Winter collection: coats and boots”), and you can style that text (font, color, size on the img tag) so it looks deliberate when shown in place of the image.
  • Never send a message that is one big image: it is unreadable with images off, inaccessible, and one of the clearest spam signals there is (lesson 12).

Image proxies, privacy and tracking pixels

  • Gmail has loaded all external images through its own proxy since December 2013: the sender never sees the reader’s IP address, and Gmail caches images, so it’s fetched once, not at every open.
  • Apple Mail Privacy Protection (since iOS 15 and macOS Monterey, September 2021) preloads remote images through Apple’s proxy for users who enable it, whether or not they open the message.
  • A tracking pixel is a tiny hosted image with a unique URL per recipient; fetching it counts as an “open”. Between proxies, preloading, blocking and privacy laws, open rates are now an unreliable estimate. Measure clicks, conversions and replies instead, and mention tracking in your privacy notice where the law requires it.

Images in email signatures

Logos in signatures cause the familiar “image001.png” attachments: Outlook embeds them as CID images, and some recipients’ clients show them as attachments, which makes every message look like it has a file attached. Options: a small hosted logo (shows as a link-loaded image, blocked in some clients until allowed), a text-only signature (always works, often the best choice for internal mail), or a central signature service that adds signatures on the server, consistently. Keep signatures small, and never make the whole signature one image.

(Your logo next to your messages in the inbox list is something else: that’s BIMI, in lesson 11.)