An SMTP Conversation by Hand
The whole protocol, typed into a socket.
Five commands send a message. All of them are text.
Connecting
The server speaks first, before you type anything:
QUIT220 means ready. Every reply is a three-digit code and some text, and only the code is
for your program.
Each block below is sent with printf '%s\r\n' into nc localhost 1025, as chapter 1
did. The blocks show the conversation; the shell carrying it never changes.
EHLO
Introduce yourself and the server lists what it supports:
EHLO test.local
QUITThe - after 250 means more lines follow; a space marks the last.
The older HELO gets one line and no capabilities:
HELO test.local
QUITMAIL FROM and RCPT TO
The envelope. Who the message is from:
EHLO test.local
MAIL FROM:<[email protected]>
QUITAnd who for — one command per recipient, each answered separately:
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
QUITThe angle brackets are required. These two addresses are the envelope, and chapter 3
shows they are unrelated to the From: a reader sees.
DATA
DATA switches from commands to content:
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Hello
This is the body.
.
QUIT354 is the only 3xx in SMTP. Everything after it is the message — headers, blank line,
body, which is chapter 3.
Headers go after DATA: Subject: is message content, and the server never reads it
to decide anything.
The lone dot
A line containing only . ends the message, and the queue ID is the receipt:
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Short
body
.
QUITRecord that ID — it is what a server’s logs are searched by, and the only handle you have on a message once it has left.
Dot-stuffing
A body line starting with . must be sent as two. Watch what arrives:
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Dots
normal line
..this began with one dot
.
QUITcurl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
if "Dots" in str(x["Content"]["Headers"].get("Subject", ""))][0]
print(repr(m["Content"]["Body"]))'Two dots went in, one came out — the server strips the extra one.
Reply codes
The first digit is the whole decision:
| Class | Means | Do |
|---|---|---|
2xx |
accepted | continue |
3xx |
send more | only 354, after DATA |
4xx |
temporary failure | queue and retry |
5xx |
permanent failure | stop, and report |
An unknown command gets a 5xx:
NOTACOMMAND
QUITPort 25
Port 25 is server-to-server, expects no authentication, and many networks block it outbound to stop compromised machines relaying spam.
nc -v -z -w 5 gmail-smtp-in.l.google.com 25Use 587 from an application. It requires authentication, and chapter 6 is the TLS that protects the password.