An SPF record is a single DNS TXT record, starting with v=spf1, that lists which servers may send email using your domain in the envelope sender (the Return-Path). Receivers evaluate it from left to right and return pass, fail, softfail, neutral or an error. The most common error is a PermError caused by exceeding the limit of 10 DNS lookups that RFC 7208 places on every SPF evaluation.
This guide covers how SPF evaluation works, what each mechanism and qualifier means, how to count lookups, and how to fix a record that has grown past the limit.
How SPF works
When a mail server receives a message, it looks at the domain in the SMTP MAIL FROM command (the envelope sender, also called the Return-Path or bounce address). It then queries the TXT records of that domain, finds the one that starts with v=spf1, and checks whether the connecting IP address matches any of the mechanisms in it.
A typical record looks like this:
example.com. 3600 IN TXT "v=spf1 ip4:203.0.113.10 include:_spf.google.com -all"
Read it as: "Mail for example.com may come from 203.0.113.10 or from any server Google lists. Everything else fails."
Two details trip people up:
- SPF checks the envelope sender, not the
From:header your recipients see. A message can pass SPF forbounces.esp.example.netwhile showingFrom: news@example.com. That gap is what DMARC alignment closes. - SPF breaks on forwarding. When a message is forwarded, the forwarding server's IP is not in your record, so SPF fails at the final destination. This is why DMARC also accepts an aligned DKIM pass, and why you should run DKIM alongside SPF.
SPF syntax: mechanisms, qualifiers and modifiers
An SPF record is a version tag followed by terms separated by spaces. Each term is either a mechanism (something to match against the sender IP) or a modifier (extra instructions).
Mechanisms
| Mechanism | Matches when | Costs a DNS lookup |
|---|---|---|
all |
Always. Used last as the default. | No |
ip4:203.0.113.0/24 |
The IP is in this IPv4 range | No |
ip6:2001:db8::/32 |
The IP is in this IPv6 range | No |
a or a:mail.example.com |
The IP matches an A or AAAA record of the domain | Yes |
mx or mx:example.com |
The IP matches one of the domain's MX hosts | Yes |
include:_spf.google.com |
The other domain's SPF record returns pass | Yes, plus its own lookups |
exists:%{i}._spf.example.com |
The constructed name has any A record | Yes |
ptr |
Reverse DNS of the IP ends in the domain | Yes (deprecated) |
RFC 7208 section 5.5 says ptr "SHOULD NOT be published". It is slow, unreliable, and some receivers ignore it. Replace it with ip4, ip6 or include.
Qualifiers
Each mechanism can carry a prefix that sets the result when it matches. The default is +.
| Qualifier | Result | Typical use |
|---|---|---|
+ |
pass | Implied on every mechanism, rarely written |
- |
fail | -all: anything not listed is unauthorized |
~ |
softfail | ~all: probably unauthorized, accept but mark |
? |
neutral | ?all: no assertion either way |
~all vs -all: which should you use?
With DMARC in place, the difference matters less than people think. DMARC only cares whether SPF passed and aligned; a softfail and a fail both count as "not pass". The receiver then applies your DMARC policy.
-all is the stricter statement and is a good choice once you are confident every legitimate source is listed. ~all is common during rollout. Never publish +all: it authorizes every IP on the internet to send as your domain.
Modifiers
redirect=_spf.example.comreplaces the current record with another domain's record if nothing else matched. It counts as a DNS lookup, and it is ignored if the record also contains anallmechanism.exp=explain.example.compoints to a TXT record with a human-readable explanation for failures. Rarely used.
Each modifier may appear at most once.
One SPF record per domain
A domain must have exactly one TXT record that begins with v=spf1. If a receiver finds two, the result is PermError and SPF fails for every message. This often happens when a new service tells you to "add this SPF record" and someone creates a second record instead of merging.
Wrong:
example.com. TXT "v=spf1 include:_spf.google.com ~all"
example.com. TXT "v=spf1 include:sendgrid.net ~all"
Right:
example.com. TXT "v=spf1 include:_spf.google.com include:sendgrid.net ~all"
Other TXT records (site verification tokens, for example) can sit next to the SPF record without any problem. Only records starting with v=spf1 count.
Long records and the 255-character limit
A single string inside a DNS TXT record can hold at most 255 characters. Longer SPF records must be split into several quoted strings in the same record. RFC 7208 section 3.3 says receivers join them without adding spaces, so put the space inside one of the strings:
example.com. TXT ( "v=spf1 ip4:203.0.113.0/24 ip4:198.51.100.0/24 "
"include:_spf.google.com -all" )
Most DNS dashboards split long values for you. Check the result with an SPF checker to confirm no space was lost at the join.
The 10 DNS lookup limit
RFC 7208 section 4.6.4 limits SPF evaluation to 10 terms that require DNS queries. The terms that count are:
includeamxptrexists- the
redirectmodifier
ip4, ip6 and all cost nothing. The count is cumulative across the whole evaluation, so every lookup inside an included record counts against your 10 as well. Go over and the result is PermError. Under DMARC, a PermError is not a pass, so SPF stops helping you on every message.
There are two related limits:
- Each
mxmechanism may resolve at most 10 address records for the MX hosts. More than that is also a PermError. - Void lookups (queries that return no records or NXDOMAIN) should be limited to two. A third typically produces PermError. Stale
include:entries for services you no longer use are the usual cause.
Worked example: counting lookups
Here is a realistic record for a company on Google Workspace that also uses SendGrid, Mailgun and Mailchimp:
example.com. TXT "v=spf1 mx a include:_spf.google.com include:sendgrid.net include:mailgun.org include:servers.mcsv.net ~all"
The lookups, using the providers' published records at the time of writing (they change, so always recount):
| Term | Lookups | Why |
|---|---|---|
mx |
1 | Query the MX hosts of example.com |
a |
1 | Query the A/AAAA records of example.com |
include:_spf.google.com |
1 | Contains only ip4/ip6 ranges |
include:sendgrid.net |
2 | Itself, plus a nested include:ab.sendgrid.net |
include:mailgun.org |
5 | Itself, _spf.mailgun.org, which includes _spf1 and _spf2, plus _spf.eu.mailgun.org |
include:servers.mcsv.net |
1 | Contains only ip4 ranges |
| Total | 11 | One over the limit: PermError |
A few things to notice. First, include cost varies wildly: one Mailgun include costs as much as five Google includes. Second, mx and a are often added out of habit. If example.com receives mail at Google, the MX hosts are Google's inbound servers, which do not send your outbound mail. Removing mx and a brings this record to 9, which is valid.
Third, many ESPs do not need to be in your root SPF record at all. When an ESP uses its own bounce domain (or a subdomain you delegate, such as bounces.example.com), SPF is evaluated against that domain, not yours. Check your ESP's documentation: if it sets a custom Return-Path on a subdomain, the include belongs on that subdomain's record.
How to fix an SPF PermError
Work through these in order. Each one is less risky than the next.
- Remove what you do not use. Old CRMs, retired helpdesks and trial ESPs accumulate. Remove their includes.
- Drop
mx,aandptrunless they are needed. Replace them with explicitip4/ip6ranges for servers you control. - Move senders to subdomains. Put marketing on
news.example.comand transactional mail onmail.example.com, each with its own SPF record and its own 10-lookup budget. This also isolates reputation. - Rely on DKIM where SPF cannot align. If a service signs with DKIM using your domain, DMARC can pass on DKIM alone. SPF for that service then matters less.
- Flatten, as a last resort.
SPF flattening tradeoffs
Flattening replaces include: terms with the IP ranges they resolve to:
example.com. TXT "v=spf1 ip4:74.125.0.0/16 ip4:209.85.128.0/17 ip4:167.89.0.0/17 ... ~all"
That costs zero lookups, but it has real downsides:
- It goes stale silently. When a provider adds a new IP range, your record does not change, and mail from that range fails SPF. Providers do change their ranges without notice.
- Records get long. You may need many 255-character strings, and very large responses can push DNS over UDP size limits on some resolvers.
- You lose provider intent. An
includesays "whatever Google says". A flattened list says "whatever Google said on the day you copied it".
If you flatten, automate it: regenerate on a schedule from the source records and alert on changes. A one-time manual flatten is worse than a tidy record with includes.
Common SPF mistakes
- Two SPF records on the same name. Merge them.
- Using
+allor?allat the end. Both make SPF meaningless. - Including a domain that has no SPF record. That is a void lookup and, past two of them, a PermError.
- Expecting SPF to protect the From address. It does not. Only DMARC ties authentication to the visible From domain.
- Forgetting subdomains. SPF is not inherited. If
mail.example.comsends mail, it needs its own record. If a subdomain never sends, publishv=spf1 -allon it. - Using the deprecated SPF record type (type 99). Only TXT is used now.
- Not publishing anything for non-sending domains. Parked domains are easy to spoof. Give them
v=spf1 -alland a DMARC record withp=reject.
Does SPF alone stop spoofing?
No. SPF authorizes servers for the envelope sender, which recipients never see. An attacker can pass SPF on their own domain while forging your domain in the From: header. DMARC requires that the SPF or DKIM domain match the From domain, which is what stops this. The DMARC setup guide walks through publishing a record and moving to enforcement.
How long does an SPF change take to work?
As long as the old record's TTL. If the TXT record has a TTL of 3600, resolvers may serve the old value for up to an hour. Lower the TTL a day before a planned change if you want faster rollouts.
Key takeaways
- Publish exactly one
v=spf1TXT record per sending domain or subdomain. - Keep the total of
include,a,mx,ptr,existsandredirectlookups at 10 or fewer, counting nested includes. - Keep void lookups at two or fewer by removing dead includes.
- End with
~allduring rollout and-allonce your sender list is complete. Never+all. - Prefer subdomains and DKIM over flattening when you run out of lookups.
- Pair SPF with DKIM and DMARC. Use the DMARC checker or the full domain scan to see all three together.
Count your lookups and catch PermErrors in seconds with the free SPF checker.