The idea: push with SMTP, pull with IMAP or POP3
An email crosses several machines, and two kinds of protocol move it. SMTP (Simple Mail Transfer Protocol) pushes the message forward: from Alice's mail app to her provider's server, and from that server to the receiver's server. There it is saved in a mailbox. Later Bob's mail app pulls it out with IMAP or POP3. The two mail apps never talk to each other.
The canvas follows one message, Lunch on Friday?, from alice@sender.example to bob@receiver.example. Each command and each reply is a box that flies from one machine to the other, and the panel below writes the session down line by line, as a mail server's log would (C: client, S: server). The journey has three stages; Next stage runs the next one.
Submission on port 587: STARTTLS, then AUTH
Alice's app does not deliver mail itself. It submits it to her provider's server, smtp.sender.example, on port 587, the submission port (RFC 6409). The server speaks first (220). The app says EHLO and the server lists its extensions. STARTTLS turns the same TCP connection into a TLS connection; after that the app must say EHLO again, and only now is AUTH offered. AUTH PLAIN sends the user name and password in base64, which is an encoding, not encryption: TLS is what keeps it secret. Port 587 relays only for users who logged in, so strangers cannot use the server to send spam (Demo 1: submit on port 587).
Many providers also accept submission on port 465, where TLS starts with the first byte instead of after STARTTLS (RFC 8314 now prefers it). The SMTP commands inside are the same.
Finding the receiver: the MX record
smtp.sender.example puts the message in its mail queue and then needs to know where mail for receiver.example goes. It asks DNS for the MX record (mail exchanger) of the domain after the @. The answer names a host and a preference: MX 10 mx.receiver.example. With several MX records, the server tries the lowest number first and the others as backups. It then looks up that host's address (A or AAAA record) and connects to it on port 25 (Demo 2: MX lookup, SMTP relay on port 25). DNS Resolution shows how the resolver finds such an answer.
The SMTP conversation and its reply codes
Server to server, the talk is the same as on submission, without AUTH: EHLO, STARTTLS, EHLO again, then one mail transaction: MAIL FROM, one RCPT TO per recipient, DATA, the message ending with a line that holds a single dot, and QUIT. Every command gets a three-digit reply. The first digit says what to do next:
| Reply | Meaning | On the canvas |
|---|---|---|
| 2xx | done, OK | 220 ready, 250 OK, 235 logged in, 221 bye |
| 3xx | go on, send more | 354 send the message, end with "." |
| 4xx | temporary failure: try again later | (not shown) e.g. 451, 452 mailbox full |
| 5xx | permanent failure: do not retry | 550 5.1.1 user unknown |
The extra 2.1.5 or 5.1.1 after the code is an enhanced status code (RFC 3463): class, subject, detail. A dash after the code (250-SIZE) means more lines of the same reply follow. Between servers, STARTTLS is opportunistic: if the MX did not offer it, most senders would go on in plain text. MTA-STS and DANE are ways for a domain to demand TLS.
Envelope vs headers
MAIL FROM and RCPT TO are the envelope: servers route by them, and a bounce goes to the MAIL FROM address. The From:, To: and Subject: lines after DATA are headers, part of the message, and they are what Bob's app shows. The two do not have to match, which is why a Bcc works, and also why anyone can write a fake From:. SPF, DKIM and DMARC shows how the receiver checks who really sent a message. Every relay adds a Received: header at the top, so a message carries the trace of its hops.
550 and the bounce
With the typo address bbo@receiver.example, submission still works: smtp.sender.example cannot know which mailboxes exist at another domain. The MX knows, and answers RCPT TO with 550 5.1.1 User unknown. By then the sender's server has already promised to deliver the message (it said 250 queued to Alice), so it must report the failure. It writes a bounce, a delivery status notification (RFC 3464) from MAILER-DAEMON, to the envelope sender alice@sender.example (Demo 3: wrong address). With a 4xx reply it would keep the message in the queue and retry for several days before bouncing.
Delivery to the mailbox
SMTP ends at the receiver's server; it does not push the mail to Bob's laptop, which may be off. Here the MX (Postfix) hands the message to the mailbox store (Dovecot) with LMTP (RFC 2033), a cut-down SMTP for the last local hop. Dovecot saves it in bob's INBOX with a UID, a number that never changes, and no \Seen flag yet.
IMAP vs POP3
IMAP (port 993 with TLS) works on the mailbox where it is. Each command has a tag (a1, a2 …) and its final answer repeats the tag with OK, NO or BAD; lines starting with * carry data. LOGIN logs in, SELECT INBOX opens a folder and reports how many messages EXISTS, FETCH reads a message and sets \Seen on the server. IDLE (RFC 2177) keeps the connection open so the server can push * 2 EXISTS the moment new mail arrives (Demo 4: Bob reads with IMAP). Because messages, folders and flags all stay on the server, Bob's phone sees the same INBOX, already marked read.
POP3 (port 995 with TLS) is older and simpler: USER, PASS, RETR to download, DELE to mark for deletion, QUIT. Marked messages are deleted only at QUIT (the UPDATE state). The usual setup downloads and deletes, so the only copy ends up on one device and the phone finds 0 messages (Demo 5: POP3 downloads and deletes).
| IMAP | POP3 | |
|---|---|---|
| Port (TLS) | 993 (143 + STARTTLS) | 995 (110 + STLS) |
| Mail stays on the server | yes | usually deleted after download |
| Folders and flags | on the server, shared by all devices | INBOX only, no flags |
| New mail | pushed with IDLE | the app polls |
| Replies | tagged OK / NO / BAD | +OK / -ERR |
See also SPF, DKIM and DMARC for how the MX decides whether alice@sender.example really sent this mail, the HTTPS connection for the TLS handshake that STARTTLS starts, and the TCP handshake under every connection on this page.
What the page leaves out
The TLS handshake itself, the full EHLO extension lists, PIPELINING, SIZE checks and 8BITMIME, MIME parts and attachments, several recipients and several MX hosts with fallback, 4xx retries and the queue's retry schedule, greylisting, SPF/DKIM/DMARC checks and spam filtering at the MX, MTA-STS and DANE, IMAP's UID commands, SEARCH, folders other than INBOX and the sequence-number shift after EXPUNGE, POP3's UIDL and "leave on server", and OAuth2 logins (XOAUTH2) that big providers use instead of passwords.
On the mind map
References
RFC 5321: Simple Mail Transfer Protocol (commands, reply codes, MX use)
RFC 6409: Message Submission for Mail (port 587, AUTH); RFC 3207: SMTP STARTTLS
RFC 9051: IMAP4rev2 (LOGIN, SELECT, FETCH, \Seen); RFC 2177: IMAP IDLE
RFC 1939: Post Office Protocol version 3 (USER, PASS, RETR, DELE, UPDATE state)