Sub topic 4
SPF: authorizing the servers that send for you
This chapter assumes you can read and edit DNS TXT records, know the difference between the SMTP envelope sender and the visible From header, and can read raw message headers. SPF is one of two authentication methods (the other is DKIM) that Gmail, Yahoo, and Microsoft expect bulk senders to publish. It is also the one most often broken by small mistakes.
What SPF does
Sender Policy Framework (SPF, RFC 7208) lets a domain publish a list of the servers allowed to send mail for it. A receiving server compares the IP address that connected to it against that list.
SPF does not check the From address that readers see. It checks the domain in the SMTP envelope sender, also called MAIL FROM, Return-Path, or bounce address. If the envelope sender is empty, as it is for bounce messages, the check uses the HELO name instead.
This distinction explains much of SPF's behavior in marketing email. Your ESP usually sets the envelope sender to a domain it controls, so SPF can pass for the ESP's domain while saying nothing about your From domain. Alignment, covered in the next chapter, is what connects the two.
Anatomy of a record
An SPF record is a single TXT record at the domain's apex that starts with v=spf1. The rest is a list of mechanisms, evaluated left to right. The first mechanism that matches decides the result.
example.com. TXT "v=spf1 ip4:203.0.113.25 include:_spf.esp.example.net -all"| Mechanism | Meaning | Counts toward the 10-lookup limit |
|---|---|---|
| `ip4:` / `ip6:` | Matches an address or CIDR range | No |
| `include:domain` | Evaluates another domain's SPF record and matches if it passes | Yes |
| `a` / `a:domain` | Matches the A or AAAA addresses of a host | Yes |
| `mx` | Matches the domain's mail exchangers | Yes |
| `exists:` | Matches if a DNS A lookup returns a result | Yes |
| `redirect=` | Replaces the whole policy with another domain's record | Yes |
| `all` | Matches everything; always last | No |
Each mechanism can carry a qualifier prefix. With no prefix, + (pass) is assumed.
| Qualifier | Result | Typical use |
|---|---|---|
| `+` | pass | Implied default |
| `-` | fail | `-all`: reject mail from unlisted servers |
| `~` | softfail | `~all`: accept but treat as suspicious |
| `?` | neutral | `?all`: make no statement |
The ptr mechanism is deprecated by the RFC. Do not use it. It is slow and unreliable.
Choosing between `-all` and `~all`
Both work for bulk sender compliance. The providers require that SPF or DKIM passes and aligns. They do not require a particular ending. In practice, DMARC acts on the SPF result, so a softfail and a fail usually lead to the same outcome once DMARC is enforced.
- Use
-allwhen you know every system that sends as your domain, including CRM, billing, support desk, and the marketing platform. - Use
~allwhile you are still discovering senders, typically while reading DMARC aggregate reports. - Avoid
+all. It authorizes the entire internet. Avoid?allfor the same reason, because it publishes no policy.
Setting up SPF for a marketing program
Most ESPs give you an include: value. A typical setup has two parts.
- Add the ESP's include to the SPF record on the domain that appears in the envelope sender.
- Configure a custom return-path (bounce) domain, such as
bounce.example.com, usually as a CNAME to the ESP.
Step 2 matters for compliance. Without it, the ESP uses its own domain as the envelope sender. SPF will pass, but for the ESP's domain, so it does not align with your From domain. DKIM can still carry DMARC in that case, but you lose SPF as a second path.
With a custom bounce domain, the SPF record the receiver actually evaluates lives at bounce.example.com, and the ESP usually manages it through the CNAME. Your apex record only matters for mail whose envelope sender is @example.com, such as mail from your own mail servers.
Verify what is published with the commands below. Replace the placeholder domains with your own.
dig +short TXT example.com | grep spf1
dig +short TXT bounce.example.com
dig +short CNAME bounce.example.comThe 10-lookup limit
RFC 7208 allows at most 10 DNS-querying mechanisms and modifiers during one evaluation. The count includes lookups made inside nested includes. If the receiver reaches an eleventh, the result is permerror, and the message is treated as not passing SPF.
This limit is easy to exceed, because every SaaS tool asks you to add its own include:, and each vendor's record may contain further includes.
example.com. TXT "v=spf1 include:_spf.google.com include:servers.mcsv.net include:sendgrid.net include:spf.protection.outlook.com include:mail.zendesk.com include:_spf.salesforce.com -all"Six includes here can easily expand to more than 10 lookups once each vendor's nested records are counted. Receivers also enforce a limit of two "void" lookups, which are queries that return no data or NXDOMAIN. A stale include pointing at a dead vendor domain can cause a permerror that way.
Ways to stay under the limit:
- Remove includes for vendors you no longer use.
- Replace an
include:withip4:ranges only when the vendor guarantees those addresses are stable. Vendors change them, so hard-coded ranges can silently go stale. - Send different mail streams from different subdomains, such as
news.example.comfor marketing, each with its own short SPF record. This is often the cleanest fix. - Use a flattening service if you accept the maintenance dependency.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `spf=permerror` | Two `v=spf1` records, or more than 10 lookups | Merge into one record; reduce lookups |
| `spf=none` | No record at the domain being checked, often the bounce domain | Publish the record at the envelope sender domain |
| `spf=softfail` or `fail` | Sending IP not listed | Add the vendor's include or the IP |
| `spf=pass` but DMARC fails | Envelope domain differs from From domain | Configure a custom return-path domain |
| `spf=pass` for direct mail, fail after forwarding | Forwarder's IP is not in your record | Expected; rely on DKIM |
| `spf=temperror` | DNS timeout at receiver | Usually transient; check your DNS hosting reliability |
Two further details cause trouble. A single TXT string is limited to 255 characters, so long records must be split into several quoted strings in one TXT record. Most DNS panels do this for you, but some do not. Also, DNS responses that exceed 512 bytes may fall back to TCP, which some firewalls block. Keep records short.
Reading the result in headers
The receiver records its SPF verdict in the Authentication-Results header.
Authentication-Results: mx.example.net;
spf=pass smtp.mailfrom=bounce.example.com;
dkim=pass header.d=example.com header.s=mail1;
dmarc=pass (p=NONE) header.from=example.comHere smtp.mailfrom is the envelope domain SPF was checked against. It differs from header.from, so SPF passed but did not align. DMARC passed through DKIM alone.
Why SPF alone is not enough
SPF breaks whenever a message is relayed through a server that is not in your record. Forwarding and mailing lists both do this. That is why Gmail, Yahoo, and Microsoft all expect DKIM as well, and why bulk senders need both mechanisms in place, with DMARC passing through at least one aligned method. The next chapter covers DKIM.
Exercise
Your marketing platform sends from [email protected]. Headers show spf=pass smtp.mailfrom=esp-mail.net and dkim=pass header.d=example.com. A colleague says SPF is fine, so nothing needs to change for DMARC. What is wrong with that conclusion, and what would you configure so that SPF also contributes to DMARC?
Show answer
SPF passed for esp-mail.net, the ESP's own envelope domain. It did not pass for example.com, so it is not aligned with the From domain and cannot satisfy DMARC. DMARC currently passes only through aligned DKIM. To make SPF contribute, set up a custom return-path domain such as bounce.example.com (usually a CNAME to the ESP) so the envelope sender is in your domain, and confirm the SPF record at that domain authorizes the ESP.
Exercise
A domain's SPF record includes seven vendors and starts returning spf=permerror after a new vendor is added. Name the likely cause and two ways to fix it.
Show answer
The evaluation now exceeds the limit of 10 DNS-querying mechanisms, counting nested includes. Fixes: remove includes for unused vendors, and move a mail stream such as marketing to its own subdomain with a separate, shorter SPF record. Using stable ip4 ranges or a flattening service also works, with a maintenance cost.