SMTP
Simple Mail Transfer Protocol
How email is sent and relayed.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
SMTP moves a message from a sender to a recipient’s mail server, often via several relays. It is a text dialogue: the client states who the mail is from and to, then sends the message, and the server answers each step with a numeric code.
Receiving and reading mail is a different job, done by POP3 or IMAP.
1.2Key ideas
The handful of ideas everything else follows from.
Envelope versus headers
MAIL FROM and RCPT TO are routing instructions. The From and To headers are what the reader sees. They can differ.
Store and forward
Each server queues and retries if the next hop is down.
STARTTLS
Upgrades a plain connection to TLS after EHLO. Port 465 uses TLS from the start.
Authentication
SPF, DKIM and DMARC let receivers check that a message really comes from the claimed domain.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why can the visible From differ from MAIL FROM?
- What ends the DATA section?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
The server greets.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
SMTP is a text dialogue on port 25 (server to server) or 587 (mail submission). Each line ends with CR LF.
Introduces the client and asks for extensions.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| EHLO | 0 | 25 | 45 48 4c 4f 20 63 6c 69 … | |
| MAIL FROM | 25 | 31 | 4d 41 49 4c 20 46 52 4f … | |
| RCPT TO | 56 | 27 | 52 43 50 54 20 54 4f 3a … | |
| DATA | 83 | 6 | 44 41 54 41 0d 0a | |
| Message | 89 | 30 | 53 75 62 6a 65 63 74 3a … | |
| QUIT | 119 | 6 | 51 55 49 54 0d 0a |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Parsing SMTP replies
Mail servers talk in numbered lines, and the first digit says what to do.
1/** "250-" means more lines follow. "250 " with a space is the last line. */2export function parseReply(text: string) {3 const lines = text.split("\r\n").filter(Boolean);4 const last = lines[lines.length - 1];5 const code = Number(last.slice(0, 3));6 return {7 code,8 complete: last[3] === " ",9 lines: lines.map((l) => l.slice(4)),10 // 2xx worked, 3xx wants more input, 4xx try later, 5xx do not retry.11 retryLater: code >= 400 && code < 500,12 };13}14 15/** A lone "." ends a message, so a body line starting with a dot gets a second one. */16export function dotStuff(body: string): string {17 const stuffed = body.split("\r\n").map((l) => (l.startsWith(".") ? "." + l : l)).join("\r\n");18 return stuffed + "\r\n.\r\n";19}last[3] === " "- A hyphen means more lines follow, and a space means this is the last.
code >= 400 && code < 500- Temporary, so queue and retry. 5xx is permanent.
dotStuff- A lone dot ends a message, so body lines starting with a dot get escaped.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why can the visible From differ from MAIL FROM?
What ends the DATA section?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Open relays get abused for spam.
- Mail to a large provider needs correct reverse DNS and SPF/DKIM or it lands in spam.
- 4xx means retry later, 5xx means give up and bounce.