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

Debugging Email

A method for any delivery problem, and a checklist.

Given a symptom: which command, and which chapter.

The method

Four layers, top down. Each rules out everything above it:

  1. Does DNS say where the mail should go?
  2. Can you reach that server?
  3. Does the SMTP conversation complete?
  4. Does the message pass authentication?

Check DNS first

Four lookups. The first three say where mail goes and whether you may send it:

d=google.com
printf 'MX    '; dig +short MX $d | sort -n | head -1
dig +short TXT $d | grep -o 'v=spf1[^"]*' | sed 's/^/SPF   /'
dig +short TXT _dmarc.$d | grep -o 'v=DMARC1; p=[a-z]*' | sed 's/^/DMARC /'
OutputMX 10 smtp.google.com.SPF v=spf1 include:_spf.google.com ~allDMARC v=DMARC1; p=reject

The fourth is reverse DNS for the IP you send from:

dig +short -x 8.8.8.8
Outputdns.google.

Reach the server

Before SMTP, check the socket — a refusal is instant, a block hangs:

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

If that hangs, your network blocks port 25 — chapter 2, and nothing about your mail setup is wrong.

Reproduce the conversation

Remove the library. Chapter 2’s five commands say whether the server accepts it at all:

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

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

If this works and your application does not, the difference is in what it sends. Capture that the same way and compare the two conversations.

For TLS problems, s_client separates the handshake from everything above it:

terminal
openssl s_client -starttls smtp -brief \
    -connect gmail-smtp-in.l.google.com:25 </dev/null 2>&1 \
  | grep Verification
OutputVerification: OK

Read the full headers

Every client hides them by default, and every client can show them:

Client Where
Gmail ⋮ → Show original
Outlook File → Properties
Apple Mail View → Message → Raw Source
Thunderbird Ctrl+U

Chapter 3’s Received headers stack newest-first, so the path reads bottom to top and the gaps are where the message waited.

Authentication-Results

The fastest diagnostic in the course. A receiving server records its own verdicts in a header — here is one, written by hand:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
Subject: AuthRes
Authentication-Results: mx.example.com;
 spf=pass [email protected];
 dkim=pass header.d=test.local;
 dmarc=pass header.from=test.local

body
.
QUIT
Output221 Bye

It is folded across four lines — a leading space continues the previous header, which is how any long header is written. Split on the semicolons:

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 "AuthRes" in str(x["Content"]["Headers"].get("Subject", ""))][0]
for part in m["Content"]["Headers"]["Authentication-Results"][0].split(";"):
    print(part.strip())'
Outputmx.example.comspf=pass [email protected]dkim=pass header.d=test.localdmarc=pass header.from=test.local

That is your chapter 5 setup graded by the party that matters. Mail yourself at any large provider and read it.

Interpreting a bounce

Three things in order: the status code, the diagnostic text, and which hop produced it.

python3 -c '
names = {"1": "address", "2": "mailbox", "4": "network", "7": "policy"}
for c in ["5.1.1", "4.4.1", "5.7.1"]:
    cls, sub, _ = c.split(".")
    kind = "permanent" if cls == "5" else "temporary"
    print(f"{c} {kind}, {names[sub]} problem")'
Output5.1.1 permanent, address problem4.4.1 temporary, network problem5.7.1 permanent, policy problem

5.7.1 is the DMARC rejection. After a DNS change it usually means the record is wrong or has not propagated — not the message.

The checklist

Ten checks, and the first four are not your application:

# Check Command Chapter
1 Does the domain have an MX? dig +short MX d 1
2 Does the sending IP have a PTR? dig +short -x ip 1
3 Is there an SPF record, under ten lookups? dig +short TXT d 5
4 Is there a DMARC record? dig +short TXT _dmarc.d 5
5 Is the port reachable? nc -v -z host 25 2
6 Does TLS verify? openssl s_client -starttls smtp 6
7 Does the conversation complete? printf ... | nc 2
8 What does Authentication-Results say? read the headers 5
9 Is the bounce 4xx or 5xx? read the DSN 8
10 Is it arriving but filed as spam? reputation, not protocol 8