Skip to content
Sign inGet all access
Chapter 2·12 minFree

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:

QUIT
Output220 mailhog.example ESMTP MailHog221 Bye

220 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
QUIT
Output220 mailhog.example ESMTP MailHog250-Hello test.local250-PIPELINING250 AUTH PLAIN221 Bye

The - after 250 means more lines follow; a space marks the last.

The older HELO gets one line and no capabilities:

HELO test.local
QUIT
Output220 mailhog.example ESMTP MailHog250 Hello test.local221 Bye

MAIL FROM and RCPT TO

The envelope. Who the message is from:

EHLO test.local
MAIL FROM:<[email protected]>
QUIT
Output250 Sender [email protected] ok221 Bye

And who for — one command per recipient, each answered separately:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
QUIT
Output250 Recipient [email protected] ok250 Recipient [email protected] ok221 Bye

The 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.
.
QUIT
Output354 End data with <CR><LF>.<CR><LF>250 Ok: queued as <id>@mailhog.example221 Bye

354 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
.
QUIT
Output250 Ok: queued as <id>@mailhog.example221 Bye

Record 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
.
QUIT
Output250 Ok: queued as <id>@mailhog.example221 Bye
curl -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"]))'
Output'normal line\r\n.this began with one dot'

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
QUIT
Output220 mailhog.example ESMTP MailHog500 Unrecognised command221 Bye

Port 25

Port 25 is server-to-server, expects no authentication, and many networks block it outbound to stop compromised machines relaying spam.

terminal
nc -v -z -w 5 gmail-smtp-in.l.google.com 25
Output$ nc -v -z -w 5 gmail-smtp-in.l.google.com 25Connection to gmail-smtp-in.l.google.com port 25 [tcp/smtp] succeeded!

Use 587 from an application. It requires authentication, and chapter 6 is the TLS that protects the password.