Sending Mail That Arrives
Delivery, bounces, and the practical checklist.
Every chapter so far was mechanism. This one is what to do with it.
Why your mail is marked spam
Three factors, in this order:
| Factor | Weight | Fixed by |
|---|---|---|
| Reputation of the sending IP and domain | most | time, and not sending junk |
| Authentication | a lot | chapter 5, in an afternoon |
| Content | least | not writing like a spammer |
Authentication is the only one you can fix today, and it is necessary rather than sufficient — a perfectly authenticated message from a bad IP still lands in spam.
Queueing and retry
A 4xx means try again. The sender queues and retries on a backoff — minutes, then hours,
for up to five days.
This is why “the email never arrived” sometimes resolves itself — a loaded server answers
4xx and the message turns up forty minutes later.
Hard versus soft bounces
Chapter 2’s reply codes, one layer up:
python3 -c '
for c in ["550", "451"]:
kind = "hard" if c[0] == "5" else "soft"
act = "remove the address" if c[0] == "5" else "keep retrying"
print(c, kind, "", act)'| Code | Means | Do | |
|---|---|---|---|
| Hard | 5xx |
permanent | remove the address |
| Soft | 4xx |
temporary | keep retrying |
Reading a DSN
A bounce is a message, and a well-formed one is multipart/report with a machine-readable
part. Send yourself one:
A real bounce uses MAIL FROM:<>, the null sender, so a bounce of a bounce cannot loop.
MailHog rejects it, so this one is sent from an address.
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
Subject: Undelivered Mail Returned to Sender
Content-Type: multipart/report; report-type=delivery-status; boundary="BB"
--BB
Content-Type: text/plain
Delivery failed permanently.
--BB
Content-Type: message/delivery-status
Final-Recipient: rfc822; [email protected]
Action: failed
Status: 5.1.1
Diagnostic-Code: smtp; 550 5.1.1 User unknown
--BB--
.
QUITThe second part is the one to parse:
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 "Undelivered" in str(x["Content"]["Headers"].get("Subject", ""))][0]
for l in m["Content"]["Body"].split("\n"):
if l.startswith(("Final-Recipient:", "Action:", "Status:",
"Diagnostic-Code:")):
print(l.strip())'Status: 5.1.1 is class, subject, detail. The first digit is chapter 2’s 4-or-5 decision:
python3 -c '
for c in ["5.1.1", "4.2.2", "5.7.1"]:
cls, sub, det = c.split(".")
kind = "permanent" if cls == "5" else "temporary"
print(f"{c} {kind:9} subject {sub}")'Subject 1 is the address, 2 the mailbox, 4 the network, 7 policy. 5.7.1 is what
failing DMARC returns.
Silent failures
A message can be accepted with 250, pass all three checks, and never be seen. No
protocol mechanism reports that.
Greylisting
A deliberate 4xx on first contact. A real sender retries; a spam script does not.
It delays every legitimate first message by minutes. Less common than it was, but it explains a slow first email to a new domain.
The setup checklist
Six things, and every one was demonstrated earlier:
| # | Check | Chapter |
|---|---|---|
| 1 | MX record points somewhere real |
1 |
| 2 | PTR record for the sending IP |
1 |
| 3 | SPF lists your sender, under ten lookups | 5 |
| 4 | DKIM signing, with the key published | 5 |
| 5 | DMARC at p=none, then tightened |
5 |
| 6 | From: is a real, monitored address |
3 |
Items 1, 3 and 5 are three dig commands. Run them against any domain:
d=github.com
printf 'MX '; dig +short MX $d | sort -n | head -1
dig +short TXT $d | grep -o 'v=spf1[^"]*' | cut -c1-26 | sed 's/^/SPF /'
printf 'DMARC '; dig +short TXT _dmarc.$d | grep -oE 'p=[a-z]+' | head -1A domain with none of it answers with nothing at all:
d=neverssl.com
printf 'SPF '; dig +short TXT $d | grep -c 'v=spf1' | tr -d ' '
printf 'DMARC '; dig +short TXT _dmarc.$d | grep -c . | tr -d ' 'Transactional versus bulk
Keep them apart: a campaign collecting complaints should not stop password resets:
| Transactional | Bulk | |
|---|---|---|
| Example | password reset | newsletter |
| Volume | low, triggered | high, scheduled |
| Complaints | near zero | some, always |
| Domain | mail.example.com |
news.example.com |
A separate subdomain gives each its own reputation, and chapter 5’s sp= covers both.
List hygiene
Remove hard bounces immediately and complainers permanently. A bought or stale list damages a domain faster than any wording.
List-Unsubscribe gives the reader a one-click exit that is not a spam complaint:
EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
Subject: Unsub
List-Unsubscribe: <https://example.com/u/abc>, <mailto:[email protected]>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
body
.
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 "Unsub" in str(x["Content"]["Headers"].get("Subject", ""))][0]
h = m["Content"]["Headers"]
for k in ("List-Unsubscribe", "List-Unsubscribe-Post"):
print(k + ":", h[k][0])'Provider or self-host
Use a provider unless deliverability is your product.
A provider owns warmed IPs, handles bounces and feedback loops, and is already trusted. Self-hosting means earning that from zero.
The protocol knowledge from this course is what lets you debug a provider: read its headers, check its SPF include, tell whose problem it is.