Day 3 · Sender authentication
SPF explained: syntax, the 10-lookup limit and common mistakes
Course contents
SMTP lets anyone claim any sender address. SPF (Sender Policy Framework, RFC 7208) is the first of three mechanisms that fix this: a domain publishes, in DNS, the list of IP addresses that are allowed to send mail for it, and a receiver checks the connecting IP against that list. It is simple, almost universal, and widely misunderstood, mostly because of one detail: which domain it checks.
What SPF checks, and what it doesn’t
When a server connects and says MAIL FROM:<bounces@mail.shop.example>, the receiver:
- takes the domain of the envelope sender (
mail.shop.example), - looks up its SPF record (a TXT record starting with
v=spf1), - checks whether the connecting IP address is on the list,
- records the result, typically in an
Authentication-ResultsorReceived-SPFheader.
Three consequences people miss:
- SPF checks the envelope sender (
MAIL FROM, which becomesReturn-Path), not theFrom:header the reader sees. A phisher can pass SPF perfectly with their own domain inMAIL FROMwhile showing your domain inFrom:. Tying the two together is DMARC’s job (lesson 10). - For bounces, the envelope sender is empty (
<>), so SPF checks the domain from theEHLO/HELOname instead. That’s one more reason a server’s HELO name should be a real name in a domain with SPF. - SPF is about the last hop: the IP that connected to the receiver. When a message is forwarded, the forwarding server’s IP isn’t in the original sender’s SPF, and SPF fails. That’s normal and expected, and it’s why DKIM exists.
Reading an SPF record
v=spf1 ip4:192.0.2.0/24 ip6:2001:db8::/32 include:_spf.google.com include:sendgrid.net -all
A record is v=spf1 followed by terms, read left to right; the first mechanism that matches the IP decides the result.
| Mechanism | Matches if the IP… | DNS lookup? |
|---|---|---|
ip4:192.0.2.0/24, ip6:2001:db8::/32 | is in this range | no |
a, a:host.example.com | is an A/AAAA address of the domain or host | yes |
mx, mx:example.com | is an address of one of the domain’s MX hosts | yes (plus one per MX host) |
include:other.example | gets a pass from other.example’s SPF record | yes (plus whatever that record costs) |
exists:%{i}.spf.example | makes this name resolve (used with macros, below) | yes |
ptr | has reverse DNS in the domain. Deprecated: slow and unreliable, don’t use | yes |
all | always matches. Goes last, as the default | no |
Each mechanism can have a qualifier in front, which says what a match means:
| Qualifier | Result | Meaning |
|---|---|---|
+ (the default) | pass | Authorized |
- | fail | Not authorized: reject or treat as forged |
~ | softfail | Probably not authorized: accept, but mark as suspicious |
? | neutral | No statement either way |
Two modifiers complete the syntax: redirect=other.example (if nothing matched, use that domain’s record instead; Gmail uses it: v=spf1 redirect=_spf.google.com) and the rarely used exp= (an explanation text for failures).
The possible results of a check are pass, fail, softfail, neutral, none (no SPF record), temperror (a DNS timeout; the receiver may defer) and permerror (a broken record: a syntax error, two SPF records, or too many lookups). A permerror is treated like having no SPF at all, or worse, so a broken record is a real outage for your authentication.
~all or -all?
The end of the record says what to do with every IP not listed:
-all(fail) is the strict statement: “nothing else sends for us”. Receivers may reject outright.~all(softfail) says “nothing else should, but don’t reject on SPF alone”. Google Workspace’s own instructions use~all, Microsoft 365’s use-all.?alland+allare useless and dangerous respectively:+allauthorizes every IP on the Internet.
Once a domain has DMARC at enforcement, the DMARC policy is what decides, and the difference between ~all and -all matters less. There is one argument for ~all even then: a few receivers reject on an SPF -all failure before they evaluate DKIM and DMARC, which loses forwarded mail that DKIM would have saved. A sensible path: start with ~all while you are discovering all your senders, and move to -all when you are confident the list is complete, or stay on ~all with DMARC p=reject doing the enforcement.
The 10-lookup limit
Evaluating SPF must not turn into a DNS storm (or a denial-of-service tool), so RFC 7208 limits it: the mechanisms and modifiers that need DNS (include, a, mx, ptr, exists, redirect) may cause at most 10 DNS lookups in total, counting everything inside included records. One more lookup and the result is permerror. There is a second, quieter limit: at most two “void” lookups (names that don’t exist or have no records), to catch records that point at dead domains.
ip4 and ip6 cost nothing, which is why big providers publish their ranges directly, and why every include: you add for a new SaaS tool is a small budget decision. A record for a company with Microsoft 365, Google, a helpdesk, a CRM, a newsletter tool and an ESP is easily at the limit, as the lab below shows with a real one.
How to stay under 10:
- Remove what’s unused. Old ESPs and tools you no longer use are the most common waste. Also remove
aandmxunless those hosts really send mail. - Move senders to subdomains. If your marketing tool sends as
news.example.comand your app asmail.example.com, each subdomain has its own SPF record and its own 10 lookups. This is the clean solution, and it isolates reputation too. - Flatten carefully. Replacing an
include:with the IP ranges it currently contains saves lookups, but providers change their IPs without telling you, and a stale flattened record silently breaks. Only flatten with automation that re-checks daily, or not at all.
Macros, briefly
exists: and a few other places accept macros that the receiver expands: %{i} the sending IP, %{s} the envelope sender, %{l} its local part, %{d} the domain, %{h} the HELO name. They allow per-IP or even per-user policies served by a special DNS server. You will mostly meet them in large providers’ records; you rarely need to write one.
Forwarding breaks SPF
Bob has bob@university.example forwarded to his Gmail. A message from alice@example.com arrives at the university, whose server forwards it to Gmail unchanged. Gmail sees: envelope sender alice@example.com, connecting IP the university’s server. That IP is not in example.com’s SPF record, so SPF fails, even though nothing is wrong.
There are two fixes, both on the forwarder’s side, covered in lesson 11: SRS (the forwarder rewrites the envelope sender to its own domain, so SPF is checked against the forwarder) and ARC (the forwarder records and signs the results it saw before forwarding). And one fix on your side: make sure all your mail is DKIM-signed, because a DKIM signature survives plain forwarding.
Common SPF mistakes
| Mistake | What happens | Fix |
|---|---|---|
Two TXT records starting with v=spf1 | permerror, no SPF at all | Merge them into one |
| More than 10 lookups | permerror | Remove unused includes, move senders to subdomains |
include: of a domain that has no SPF record | permerror (and a void lookup) | Check every include with dig; follow the provider’s documentation exactly |
+all, or a huge shared range such as a whole cloud provider | Anyone on that network can send as you | List only what you use; use the ESP’s include, not raw cloud ranges |
| Adding the ESP’s include when the ESP sends with its own envelope sender | Your SPF is never checked for that mail; it doesn’t help DMARC | Set up a custom return-path / MAIL FROM domain at the ESP (bounces.example.com with a CNAME or MX they give you) |
| Forgetting IPv6 | Mail from an IPv6 address of your own server fails | Add ip6: ranges, or send over IPv4 only |
Using ptr | Slow, may be ignored, counts against the limit | Replace with ip4/ip6 |
Typos: ip4:192.0.2.0/33, include: _spf.google.com (space), smart quotes | permerror | Validate after every change |
| An SPF record only on the root, but mail sent from a subdomain | none for the subdomain | Every domain that appears in MAIL FROM (or HELO) needs its own record |
What the receiver writes
After checking, the receiver records the result. A modern receiver uses Authentication-Results (RFC 8601), often with the older Received-SPF as well:
Received-SPF: pass (google.com: domain of bounces+8f2@mail.shop.example designates 198.51.100.25
as permitted sender) client-ip=198.51.100.25;
Authentication-Results: mx.google.com;
spf=pass (google.com: domain of bounces+8f2@mail.shop.example designates 198.51.100.25
as permitted sender) smtp.mailfrom=bounces+8f2@mail.shop.example
smtp.mailfrom names the domain that was checked. When you investigate a DMARC failure, compare that domain with the From: header: if they don’t match, SPF can pass and still not help DMARC. That comparison, alignment, is the center of lesson 10.