SMTP · Simple Mail Transfer Protocol

How email is sent and relayed.

10 · Application · TCP 25 / 587 / 465

SMTP

Simple Mail Transfer Protocol

How email is sent and relayed.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a SMTP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why can the visible From differ from MAIL FROM?
  2. What ends the DATA section?

The answers are in part 4.

Part 3 of 5

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.

Client
Server
220 ready
EHLO client.example.org
MAIL FROM / RCPT TO
DATA … .
250 queued
Step 1 of 5

The server greets.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

Client commandsServer replies

SMTP is a text dialogue on port 25 (server to server) or 587 (mail submission). Each line ends with CR LF.

000045484c4f20636c69656e742e6578616dEHLO client.exam
0010706c652e6f72670d0a4d41494c204652ple.org··MAIL FR
00204f4d3a3c616c696365406578616d706cOM:<alice@exampl
0030652e6f72673e0d0a5243505420544f3ae.org>··RCPT TO:
00403c626f62406578616d706c652e636f6d<bob@example.com
00503e0d0a444154410d0a5375626a656374>··DATA··Subject
00603a2048656c6c6f0d0a0d0a486920426f: Hello····Hi Bo
0070622e0d0a2e0d0a515549540d0ab.··.··QUIT··
FieldEHLO
Length25 bytes
TextEHLO client.example.org␍␊

Introduces the client and asks for extensions.

FieldOffsetBytesHex
EHLO02545 48 4c 4f 20 63 6c 69 …
MAIL FROM25314d 41 49 4c 20 46 52 4f …
RCPT TO562752 43 50 54 20 54 4f 3a …
DATA83644 41 54 41 0d 0a
Message893053 75 62 6a 65 63 74 3a …
QUIT119651 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.

smtp.tsTypeScriptCopy
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}
19 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why can the visible From differ from MAIL FROM?
They serve different purposes: one for delivery and bounces, one for display. This is why DMARC aligns them.
What ends the DATA section?
A line containing only a dot.
Part 5 of 5

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.
← HTTPSMark as understoodPOP3 →