Skip to content

Lesson 3 of 9 · 25m

MIME, Headers, and Message Construction Without Footguns

Build standards-compliant multipart messages with correct encoding, threading, unsubscribe metadata, text alternatives, and safe templating.

Badly constructed email is like badly serialized JSON: it works in the one client you tested and explodes somewhere downstream. Email is older than most of your stack and carries four decades of compatibility behavior. Message construction is not string concatenation. It is a serialization format with security, accessibility, deliverability, and client-rendering consequences.

Headers are structured data, not vibes

Use a real library. Do not interpolate user input directly into headers. Header injection is still possible wherever newline validation is sloppy:

from email.message import EmailMessage
from email.utils import formataddr, make_msgid

msg = EmailMessage()
msg["From"] = formataddr(("SudoSecurity", "support@sudosecurity.org"))
msg["To"] = formataddr(("Ada Lovelace", "ada@example.net"))
msg["Subject"] = "Your sign-in link"
msg["Message-ID"] = make_msgid("mail.sudosecurity.org")
msg["List-Unsubscribe"] = "<https://sudosecurity.org/unsubscribe?token=...>, <mailto:unsubscribe@mail.sudosecurity.org?subject=unsubscribe>"
msg["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click"

Never allow attacker-controlled display names, subjects, or recipient addresses to contain CR or LF. One newline can create new headers or split the message. A good library rejects this; verify instead of assuming.

Always send a useful plain-text alternative

HTML email is not a webpage. Many clients strip JavaScript, forms, external CSS, and modern layout. A multipart alternative gives low-bandwidth clients, accessibility tools, and security-conscious users a readable version:

msg.set_content('''Hi Ada,

Use this link within 15 minutes:
https://sudosecurity.org/login?token=...

If you did not request this, ignore this message.
''')

msg.add_alternative('''<html><body>
<p>Hi Ada,</p>
<p><a href="https://sudosecurity.org/login?token=...">Sign in securely</a> within 15 minutes.</p>
<p>If you did not request this, ignore this message.</p>
</body></html>''', subtype="html")

The order matters because MIME clients choose the last alternative they understand. Plain text first, HTML second. Keep the content equivalent. If the HTML says one thing and the text says another, spam filters and humans both lose trust.

Templates are injection boundaries

Handlebars, Jinja, React Email, or whatever you use must distinguish HTML escaping from URL encoding and plain-text output. User-controlled content in HTML email needs escaping; URLs need safe scheme enforcement. javascript: links are stripped by good clients, but your template system should never emit them.

Also consider what tracking does to trust. Open pixels and rewritten links can make security-sensitive mail look suspicious. For password resets, minimize tracking and keep the destination domain obvious.

Field notes

  • Test in Gmail web, Apple Mail, Outlook desktop, and a plain-text client. Each breaks differently.
  • Dark mode email rendering is still a mess; avoid transparent images with dark foregrounds.
  • Internationalized addresses and subjects require correct encoding, not manual UTF-8 insertion.
  • Precedence: bulk or similar hints can reduce autoresponder loops for bulk mail, but they do not replace unsubscribe controls.
  • Keep subjects truthful. Deceptive urgency both annoys users and trains spam classifiers.

Was this lesson helpful?

Sign in to rate