Day 1 · How email works
How email works: the journey of one message, step by step
Course contents
Before any detail, follow one message from start to finish. Every step in this story is a lesson later in the course, and if, by the end, you can tell the story again with all of its details, you understand email better than most people who run it.
A short history, so the design makes sense
Email is older than the Internet. People were leaving messages for each other on shared time-sharing computers in the mid-1960s, and in 1971 Ray Tomlinson sent the first message between two different computers on the ARPANET, choosing the @ sign to separate the user from the machine. The protocol everyone still uses, SMTP (Simple Mail Transfer Protocol), was published in 1982 as RFC 821, together with the message format in RFC 822. In 1986, RFC 974 taught mail to find servers through DNS MX records, and in 1992 MIME added attachments, non-English text and HTML. Today’s versions are RFC 5321 (SMTP) and RFC 5322 (the message format), both from 2008.
Three consequences of that history explain most of what follows:
- SMTP was designed for a network of trusted machines. Nothing in the original protocol checks who you are. Anyone could write any sender address, which is why spam and phishing were possible and why SPF, DKIM and DMARC had to be added decades later.
- Email is store-and-forward. Every server takes full responsibility for a message, stores it on disk, and passes it on when it can. Nothing has to be online at the same moment, which is why email survives outages that would break a phone call.
- Everything is text. SMTP is a conversation of text commands, and a message is a block of text with headers. Binary files and non-ASCII characters are encoded into text (MIME). You can read every part of it with your own eyes, and in this course you will.
The cast: who does what
Email terminology names servers by their role. One program often plays several roles, but the roles are worth keeping apart.
| Role | Full name | What it does | Examples |
|---|---|---|---|
| MUA | Mail user agent | The app a person uses to write and read mail | Outlook, Apple Mail, Thunderbird, the Gmail app, webmail |
| MSA | Mail submission agent | Accepts new mail from authenticated users, on port 587 or 465 | Postfix’s submission service, smtp.gmail.com, smtp.office365.com |
| MTA | Mail transfer agent | Moves mail between servers with SMTP, queues and retries | Postfix, Exim, Sendmail, Microsoft Exchange, Google’s and Microsoft’s own MTAs |
| MX | Mail exchanger | The MTA that receives mail for a domain, named in that domain’s MX record | gmail-smtp-in.l.google.com |
| MDA | Mail delivery agent | Puts the message into the right mailbox, often after filtering | Dovecot LDA/LMTP, procmail, Exchange’s mailbox store |
| Mailbox server | (IMAP/POP3 server, sometimes “MRA”) | Lets the MUA read the stored mail | Dovecot, Cyrus, Exchange, Gmail |
People also say mail server for the whole package, and ESP (email service provider) for companies that send mail on your behalf, such as Amazon SES, SendGrid, Mailgun or Postmark.
The story: Alice writes to Bob
Alice works at example.com and writes to bob@example.net. Each company has its own mail provider.
- Submission. Alice clicks Send. Her MUA opens a TLS connection to her provider’s submission server (
smtp.example.com) on port 587 (with STARTTLS) or 465 (TLS from the first byte), logs in with her account, and hands over the message with SMTP. If her password is wrong, the server refuses: submission always requires authentication. - Queueing. The submission server checks the message (size, sender address, rate limits, sometimes outbound spam filtering), adds a
Received:header, usually signs it with DKIM, and puts it in its queue on disk. From this moment, Alice’s provider is responsible for the message, and Alice’s app shows it in Sent. - Routing with DNS. The MTA looks at the recipient’s domain,
example.net, and asks DNS for its MX records: “which servers accept mail for example.net?” The answer is a list of host names with priorities. It then looks up those host names’ IPv4 and IPv6 addresses. - Relay. The MTA connects to the best MX on TCP port 25, upgrades the connection to TLS with STARTTLS if the receiver offers it, and transfers the message with SMTP. Server-to-server SMTP is not authenticated with a password: the receiver accepts mail for its own domains from anyone, and judges the sender in other ways.
- Checks on arrival. Bob’s MX decides what to do with the message, often before it has even finished receiving it: is the sending IP on a blocklist? Does it pass SPF, DKIM and DMARC? Does the content look like spam or carry malware? The result is accept (to the inbox or the spam folder), reject with an error code, or, rarely, accept and silently drop.
- Delivery. The MX hands the message to the delivery agent (often over LMTP, a variant of SMTP for local delivery), which applies Bob’s filters and rules and writes it into his mailbox store.
- Reading. Bob’s phone is connected to his mailbox server with IMAP (port 993) and gets a push notification. Webmail and some apps use HTTPS APIs instead (Microsoft Graph, Gmail’s API, JMAP), but the mailbox underneath is the same.
If anything goes wrong along the way, the server that holds the message at that moment is responsible for telling the sender: it sends back a bounce (a non-delivery report) to the envelope sender address.
The envelope and the letter
The single most important idea in email is that every message has two sets of addresses, exactly like a paper letter: the address on the envelope, which the post office reads, and the address in the letter’s header, which the person reads.
- The envelope is made of SMTP commands:
MAIL FROM:<...>(the envelope sender, also called the return path or bounce address) and oneRCPT TO:<...>per recipient. Servers route the message using only the envelope. - The headers (
From:,To:,Cc:,Subject:and so on) are inside the message (theDATA). Mail apps display them; servers mostly don’t route by them.
The two are independent, and that is a feature, not a bug:
- Bcc works because Bob’s address goes into a
RCPT TOcommand but is never written in any header. - A mailing list or a newsletter keeps your
From:but puts its own bounce address inMAIL FROM, so that bounces go to the list software, not to you. In the figure, bounces go tobounces+8f2@mail.shop.examplewhile Bob seesnews@shop.example. - When the message is delivered, the receiving server records the envelope sender in a new
Return-Path:header at the top, because the envelope itself is thrown away after delivery.
The same independence is what a spoofer abuses: nothing in SMTP stops anyone from writing From: ceo@your-company.com. SPF checks the envelope sender’s domain, DKIM checks a signature that names a domain, and DMARC is what ties both of them to the From: the human actually sees. Day 3 is all about that.
Ports you will meet
| Port | Protocol | Used for |
|---|---|---|
| 25 | SMTP | Server to server (MTA to MX). Never for users. STARTTLS when available |
| 587 | SMTP submission | Mail apps and applications sending through their provider: plaintext first, then STARTTLS, then AUTH |
| 465 | SMTP submission over TLS (“SMTPS”, “submissions”) | The same as 587, but TLS from the first byte (implicit TLS). Preferred by RFC 8314 |
| 143 | IMAP | Reading mail, with STARTTLS |
| 993 | IMAP over TLS | Reading mail, implicit TLS. The normal choice |
| 110 / 995 | POP3 / POP3 over TLS | Downloading mail (older, simpler) |
| 2525 | (unofficial) | Some ESPs accept submission here too, for networks that block 587 |
| 443 | HTTPS | Webmail, Exchange (EWS, MAPI over HTTP, ActiveSync), Microsoft Graph, Gmail API, JMAP, autodiscover |
Store and forward: queues, retries and delays
An MTA never loses a message on purpose. If the next hop is down, too busy, or answers with a temporary error (a 4xx reply code), the message stays in the queue and the MTA tries again later, typically after a few minutes, then less and less often. RFC 5321 recommends retrying for at least four to five days before giving up; Postfix’s default is five days. While it waits, many servers send a “delayed, still trying” warning to the sender after a few hours.
A permanent error (a 5xx code, such as “no such user”) means the sender’s MTA stops at once and sends a bounce. This difference between 4xx and 5xx is behind half of all email troubleshooting, and it gets its own section in the SMTP lesson.
Two practical consequences:
- “I sent it an hour ago and it hasn’t arrived” can be completely normal: the receiver may be greylisting (deliberately answering the first attempt with a temporary error, on the theory that real MTAs retry and spam scripts don’t) or rate limiting a new sender.
- When you migrate a domain to a new provider, mail already queued at senders around the world is still on its way to the old MX for a while. Keep the old server receiving for days, not minutes.
Email addresses, properly
An address is local-part@domain. Some rules people often get wrong:
- The domain is case-insensitive, like everywhere in DNS. The local part is, by the standard, case-sensitive, but almost no real server treats it that way. Never rely on it either way: store addresses as typed, compare them case-insensitively.
- Sub-addressing (RFC 5233):
bob+invoices@example.netis delivered tobobat most providers (Gmail, Microsoft 365, Fastmail, Postfix withrecipient_delimiter = +). It’s useful for filtering, and a reason not to reject+in sign-up forms. - Gmail ignores dots in the local part of
@gmail.comaddresses (b.o.bisbob). That’s a Gmail rule, not an email rule. - Limits: 64 octets for the local part and 255 for the domain, and in practice a whole address can’t be longer than 254 characters.
- Internationalized addresses such as
علی@مثال.ایرانorjosé@exämple.deneed two things: the domain is converted to ASCII (Punycode,xn--...) by IDNA, and a non-ASCII local part needs the SMTPUTF8 extension (RFC 6531) on every server along the path. Support is good at the big providers and patchy elsewhere. - Validating an address with a giant regular expression is a losing game. Check that it has one
@, a plausible domain with an MX (or A) record, and then send a confirmation email: only delivery proves an address works.
What runs where, in practice
Most organizations don’t run all of these roles themselves:
- Fully hosted (Google Workspace, Microsoft 365, Zoho Mail, Fastmail, Proton): the provider runs the MSA, MTA, MX, MDA and mailbox servers. You own the domain and its DNS records, and that’s where most of your work and most problems are.
- Self-hosted (Postfix + Dovecot, Exchange Server, all-in-one packages such as Mailcow or Mail-in-a-Box): you run everything, including keeping the IP’s reputation clean.
- Hybrid: hosted mailboxes for people, plus an ESP (Amazon SES, SendGrid, Postmark, Mailgun, Brevo…) for the email that applications and marketing send, plus perhaps a security gateway in front of the MX that filters incoming mail.
Whatever the mix, every system that sends mail as your domain has to be listed in your SPF record, sign with a DKIM key published in your DNS, and align with your DMARC policy. Keeping that list complete is one of the main jobs of whoever runs email.