Skip to content
Sign inGet all access
Chapter 1·8 minFree

How Email Moves

Four programs, two protocols, and where a domain's mail goes.

Sending mail is four lines of library code. Having it arrive is the subject.

The journey of one message

Your client hands the message to a submission server, which looks up the recipient’s domain and delivers it to that domain’s mail exchanger.

Nobody is waiting at the other end. Each hop stores the message and passes it on when it can, so a delay is minutes rather than milliseconds.

MUA, MSA, MTA, MDA

Four roles; you talk to two:

Role Is You
MUA your mail client use it
MSA the submission server you authenticate to send through it
MTA a server relaying between domains never touch it
MDA the thing that files mail in a mailbox never touch it

Sending and receiving are different protocols: SMTP pushes a message towards a mailbox, IMAP and POP3 pull it back out — chapter 7.

The ports

Five numbers, three of which matter to you:

Port For Encryption
25 server to server STARTTLS, optional
465 submission TLS from the first byte
587 submission STARTTLS, expected
143 IMAP STARTTLS
993 IMAP TLS from the first byte

A server to practise against

One command gives an SMTP server that accepts everything, plus a web UI:

terminal
docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
Output$ docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog$ open http://localhost:8025

Connect to it and it introduces itself before you say anything:

QUIT
Output220 mailhog.example ESMTP MailHog221 Bye

Say EHLO and it lists what it supports:

EHLO test.local
QUIT
Output220 mailhog.example ESMTP MailHog250-Hello test.local250-PIPELINING250 AUTH PLAIN221 Bye

Nothing here is sent to a real person. MailHog accepts every message and delivers none, so you cannot accidentally mail a stranger.

The parts of an address

[email protected] is a local part, an @, and a domain. The local part belongs entirely to the receiving server; only the domain is looked up:

dig +short MX gmail.com | sort -n | head -1 | cut -d' ' -f2
Outputgmail-smtp-in.l.google.com.

MX records

The domain says where its mail goes, lowest number first:

dig +short MX gmail.com | sort -n
Output5 gmail-smtp-in.l.google.com.10 alt1.gmail-smtp-in.l.google.com.20 alt2.gmail-smtp-in.l.google.com.30 alt3.gmail-smtp-in.l.google.com.40 alt4.gmail-smtp-in.l.google.com.

Equal numbers spread load between hosts:

dig +short MX cloudflare.com | sort -n | grep '^10 '
Output10 mxa.global.inbound.cf-emailsecurity.net.10 mxb.global.inbound.cf-emailsecurity.net.

No MX record at all falls back to the A record — the web server gets the mail:

dig +short MX neverssl.com
dig +short A neverssl.com
Output34.223.124.45

A single dot means something different — the domain accepts no mail at all:

dig +short MX example.com
Output0 .

That is a null MX, and it has to be said explicitly — silence means “try the web server”.

Reverse DNS

An address maps back to a name, and receiving servers check it:

dig +short -x 8.8.8.8
Outputdns.google.

What 250 Ok does not mean

Send a message and it is accepted immediately:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Test

hi
.
QUIT
Output250 Ok: queued as <id>@mailhog.example221 Bye

queued as is the whole promise: accepted for delivery, not delivered.