What is SPF syntax?
SPF syntax is the grammar of a Sender Policy Framework record: a DNS TXT record that starts with v=spf1, followed by mechanisms (such as include, ip4 and all) that each carry an optional qualifier (+, -, ~, ?), plus up to two modifiers (redirect= and exp=). Receivers evaluate the terms left to right and stop at the first match.
This page is a reference, not an introduction. If you want the concepts first, read what SPF is and why it matters for DMARC or the SPF guide. Every rule below is checked against RFC 7208, the SPF specification, with section numbers so you can verify it yourself. To test a live record, paste the domain into our SPF analyzer.
Key takeaways
- A record must begin with exactly
v=spf1, be published as a TXT record, and exist only once per domain name. Two SPF records on one name is apermerror(RFC 7208, Section 4.5). - Only
include,a,mx,ptr,existsandredirectcount toward the limit of 10 DNS-querying terms.ip4,ip6,allandexpcost nothing (Section 4.6.4). includeonly produces a match when the included record returnspass. Afailinside an include does not stop evaluation; the receiver simply moves to the next term (Section 5.2).- Anything after
allis ignored, andredirect=is ignored entirely when the record containsall(Section 5.1). - A single syntax error anywhere in the record makes the whole result
permerror, which gives DMARC no SPF pass for any sender (Section 4.6).
What does a complete SPF record look like?
A complete record is one string with a version, a list of space-separated terms and a closing default. Here is an annotated example for a domain that sends through its own server range, Microsoft 365 and a marketing platform:
example.co.uk. 3600 IN TXT "v=spf1 ip4:192.0.2.0/28 ip6:2001:db8:10::/64 include:spf.protection.outlook.com include:_spf.mailer.example.net -all"
Read left to right:
v=spf1 version tag, must come first (0 lookups)
ip4:192.0.2.0/28 on-premises relay range (0 lookups)
ip6:2001:db8:10::/64 IPv6 range of the same relay (0 lookups)
include:spf.protection.outlook.com Microsoft 365 (1 lookup + whatever it contains)
include:_spf.mailer.example.net marketing platform (1 lookup + nested)
-all everything else fails (0 lookups)
The ABNF in Section 4.6.1 defines terms as ( 1SP ( directive / modifier ) ), so terms are separated by one or more spaces, and a directive is an optional qualifier followed by a mechanism. Mechanism and modifier names are case-insensitive, so INCLUDE: and include: behave the same.
Which mechanisms does SPF define?
RFC 7208 defines eight mechanisms: all, include, a, mx, ptr, ip4, ip6 and exists. Each one tests the connecting IP address against something, and the first one that matches decides the result. The table gives the syntax, what each matches and whether it counts toward the 10 lookup limit.
| Term | Syntax | Matches when | Counts toward 10? | RFC 7208 | Notes |
|---|---|---|---|---|---|
all |
all |
Always | No | 5.1 | Use as the last mechanism. Terms after it are ignored. |
include |
include:<domain> |
The included domain's SPF evaluation returns pass |
Yes (1, plus nested terms) | 5.2 | Domain is required. Target with no SPF record gives permerror. |
a |
a, a:<domain>, a/24, a:<domain>/24//64 |
The IP is in the domain's A (IPv4) or AAAA (IPv6) records, within the CIDR length | Yes | 5.3 | Without a domain it uses the current domain (Section 4.8). |
mx |
mx, mx:<domain>, mx/24//64 |
The IP belongs to one of the domain's MX hosts | Yes | 5.4 | Usually your inbound servers, which often do not send. |
ptr |
ptr, ptr:<domain> |
Reverse DNS of the IP validates to the domain | Yes | 5.5 | "SHOULD NOT be published." Do not use it. |
ip4 |
ip4:192.0.2.1, ip4:192.0.2.0/24 |
The IP is inside the IPv4 network | No | 5.6 | Default length is /32. Addresses only, never hostnames. |
ip6 |
ip6:2001:db8::1, ip6:2001:db8::/32 |
The IP is inside the IPv6 network | No | 5.6 | Default length is /128. |
exists |
exists:<domain> |
An A record lookup on the (usually macro-built) name returns any answer | Yes | 5.7 | Does an A lookup even for IPv6 connections. |
How do CIDR suffixes work?
A CIDR suffix widens a match from a single address to a network. On ip4 and ip6 it is written directly after the address (ip4:192.0.2.0/24). On a and mx the syntax is a "dual CIDR": one IPv4 length and an optional IPv6 length after a double slash.
a/24 every IPv4 address in the /24 around the domain's A records
a:mail.example.com/28 the /28 around mail.example.com's A records
mx//64 the IPv6 /64 around each MX host's AAAA records
ip6:2001:db8::/48 a /48 IPv6 network
Section 5.6 adds that you may not shorten an address instead of using CIDR. ip4:192.0.2 is invalid; write ip4:192.0.2.0/24.
What do the qualifiers +, -, ~ and ? mean?
A qualifier sets the result returned when its mechanism matches. It is optional and defaults to +, so include:example.net and +include:example.net are identical (Section 4.6.2). In practice the qualifier only matters on all and occasionally on a deliberate exclusion.
| Qualifier | Result on match | Typical use |
|---|---|---|
+ (default) |
pass |
Authorised senders. Never write +all. |
- |
fail |
-all to fail unlisted senders, or -ip4: to carve out one address before a wider match |
~ |
softfail |
~all while an inventory of senders is still incomplete |
? |
neutral |
?all asserts nothing; rarely useful |
If no mechanism matches and there is no redirect=, the result is neutral (Section 4.7), which is why every record should end with all or redirect=. Whether to use ~all or -all under a DMARC policy is covered in detail in SPF softfail vs hardfail with DMARC.
What are the redirect and exp modifiers?
Modifiers are name=value terms that change how a record is processed rather than matching an IP. RFC 7208 defines two: redirect= hands evaluation to another domain's record, and exp= points at an explanation string for failures. Each may appear at most once, and unrecognised modifiers must be ignored (Section 6).
| Modifier | Syntax | Effect | Counts toward 10? | RFC 7208 |
|---|---|---|---|---|
redirect |
redirect=<domain> |
If no mechanism matched, the result of the target domain's record becomes this record's result | Yes | 6.1 |
exp |
exp=<domain> |
On a fail from a mechanism match, the receiver may fetch a TXT explanation from that domain |
No (looked up later, if at all) | 6.2 |
When does redirect apply?
redirect= only takes effect after every mechanism has been evaluated without a match (Section 4.6.3). It is ignored completely when the record contains an all mechanism (Section 5.1), because all always matches first. If the target has no SPF record, the result is permerror, not none (Section 6.1).
This makes redirect= the right tool when many domains should share one policy, which is common in an MSP portfolio where a client owns a dozen brand domains that all send through the same platforms:
; each parked or brand domain
brand-one.example.com. IN TXT "v=spf1 redirect=_spf.example.com"
brand-two.example.com. IN TXT "v=spf1 redirect=_spf.example.com"
; the one record you actually maintain
_spf.example.com. IN TXT "v=spf1 include:spf.protection.outlook.com ip4:192.0.2.0/28 -all"
The policy, including the closing -all, comes from the target. Writing v=spf1 redirect=_spf.example.com ~all would silently disable the redirect.
How does include actually work?
include runs a complete SPF check against another domain and turns the result into match or no match. Only a pass from the included record counts as a match. A fail, softfail or neutral inside it is "not match", so evaluation continues with your next term. The RFC itself admits "the name 'include' was poorly chosen" (Section 5.2).
| Included record returns | include becomes |
What happens next |
|---|---|---|
pass |
match | Your qualifier is returned (normally pass) and evaluation stops |
fail, softfail, neutral |
not match | Your next term is evaluated |
temperror |
temperror |
Evaluation stops with temperror |
permerror |
permerror |
Evaluation stops with permerror |
none (no SPF record) |
permerror |
Evaluation stops with permerror |
Three consequences matter in practice. First, the -all or ~all at the end of a provider's record does not end your evaluation; it only means "not here". Second, an include pointing at a domain with no SPF record, often the result of a typo or a provider retiring a hostname, breaks SPF for the entire domain. Third, every DNS-querying term inside the included record, and inside anything it includes, counts toward your limit of 10.
In what order are SPF terms evaluated?
Mechanisms are evaluated strictly from left to right, and the first one that matches ends processing: "If it matches, processing ends and the qualifier value is returned as the result of that record" (Section 4.6.2). Modifiers can appear anywhere, but redirect= acts only at the end.
Order therefore changes meaning. These two records are not equivalent:
v=spf1 -ip4:192.0.2.66 ip4:192.0.2.0/24 -all 192.0.2.66 fails, rest of the /24 passes
v=spf1 ip4:192.0.2.0/24 -ip4:192.0.2.66 -all 192.0.2.66 passes; the exclusion is never reached
Placing ip4 and ip6 terms before includes also means a match ends evaluation before any include is queried.
What limits apply to an SPF record?
RFC 7208 sets hard limits on DNS work and soft guidance on size. Breaking a lookup limit produces permerror, so these are the numbers to watch when auditing client domains:
| Limit | Value | Result when exceeded | RFC 7208 |
|---|---|---|---|
DNS-querying terms (include, a, mx, ptr, exists, redirect), counted across all nested records |
10 | permerror |
4.6.4 |
Address lookups per mx mechanism |
10 | permerror |
4.6.4 |
Address lookups per ptr evaluation |
10 | Records beyond the first 10 ignored | 4.6.4 |
| Void lookups (NOERROR with no answers, or NXDOMAIN) | 2 (SHOULD) | permerror |
4.6.4 |
| Overall evaluation time | At least 20 seconds allowed (SHOULD) | temperror |
4.6.4 |
| Records per domain name | 1 | permerror |
3.2, 4.5 |
If a client is already near 10, see SPF too many DNS lookups for the fixes, and SPF flattening explained for MSPs for the trade-offs of replacing includes with addresses.
How long can an SPF record be?
A single TXT character-string holds at most 255 octets. Longer records are split into several quoted strings in the same TXT record, which receivers must join "without adding spaces" (Section 3.3). Put the space inside a string, not between them:
example.com. IN TXT ( "v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.0/24 "
"include:_spf.mailer.example.net -all" )
Without the trailing space after /24, the joined record reads 198.51.100.0/24include:..., which is a syntax error. Separately, Section 3.4 says the record "SHOULD remain small enough that the results of a query for it will fit within 512 octets", and gives 450 octets for the name plus all TXT records of that type as a practical guideline. Other TXT records on the same name, such as verification tokens, count toward that answer size too.
Can a domain have two SPF records?
No. A domain name "MUST NOT have multiple records" that an SPF check would select (Section 3.2), and if more than one TXT record begins with v=spf1, the check returns permerror (Section 4.5). Records must also be TXT type only; the old SPF RR type was discontinued (Section 3.1). For the merge procedure, see multiple SPF records: how to merge them.
How do SPF macros work?
Macros insert values from the current check into a domain name, written as %{letter}. They are mostly used with exists to build per-sender or per-IP lookups. Most domains never need them, and they make records harder to audit, so treat any macro in a client record as something to understand before you touch it.
| Macro | Expands to | RFC 7208 |
|---|---|---|
%{s} |
The full sender identity (MAIL FROM or HELO-derived) | 7.2 |
%{l} |
Local part of the sender | 7.2 |
%{o} |
Domain part of the sender | 7.2 |
%{d} |
The domain currently being checked | 7.2 |
%{i} |
The client IP (dotted IPv4, or dot-separated nibbles for IPv6) | 7.2, 7.3 |
%{h} |
The HELO/EHLO domain | 7.2 |
%{v} |
in-addr for IPv4, ip6 for IPv6 |
7.2 |
Transformers modify the output: r reverses the parts and a digit keeps that many right-hand parts. The RFC's own example in Section 7.4 expands %{ir}.%{v}._spf.%{d2} for client 192.0.2.3 to 3.2.0.192.in-addr._spf.example.com.
A cautious note: Section 7.3 advises against %{p} entirely and discourages s, l, o and h in mechanisms for DNS efficiency. Section 11.5.3 warns that macros let senders inject arbitrary text into receiver DNS queries, and Section 11.6 notes that exists lookups can reveal who is sending to whom in the domain owner's DNS logs. A % followed by an invalid character is a syntax error and yields permerror.
What are the most common SPF syntax errors?
Most broken records fail for one of a handful of reasons, and nearly all of them end in permerror. Any syntax error "anywhere in the record" makes the whole check fail immediately (Section 4.6), so one bad character disables SPF for every sender on the domain.
| Error | Example | What happens | Fix |
|---|---|---|---|
| Space after the colon | include: _spf.example.net |
include: has no domain, _spf.example.net is an unknown term: permerror |
include:_spf.example.net |
| Commas or semicolons as separators | include:a.example, include:b.example |
The comma makes the domain invalid: permerror |
Separate terms with single spaces |
| Two records joined together | v=spf1 include:a.example ~all v=spf1 include:b.example ~all |
Everything after the first ~all is ignored, so b.example is never checked |
One v=spf1, one all, at the end |
| Two separate TXT records | Two records both starting v=spf1 |
permerror (Section 4.5) |
Merge into one record |
Hostname in ip4 |
ip4:mail.example.com |
Not an address: permerror |
Use a:mail.example.com or the real IP range |
| Shortened address | ip4:192.0.2 |
Not permitted (Section 5.6): permerror |
ip4:192.0.2.0/24 |
all before other terms |
v=spf1 -all include:spf.protection.outlook.com |
Terms after all are ignored; all mail fails |
Move all to the end |
all plus redirect |
v=spf1 redirect=_spf.example.com ~all |
redirect is ignored (Section 5.1) |
Drop the all from the redirecting record |
+all |
v=spf1 include:_spf.example.net +all |
Every IP on the internet passes SPF for the domain | ~all or -all |
| Missing or wrong version | v=spf 1 ..., spf1 ... |
Not recognised as SPF: result none |
Begin with exactly v=spf1 |
| Include target has no record | include:_spf.retired.example |
none inside an include becomes permerror |
Remove or correct the include |
Copy-paste damage belongs on the list too: curly quotes, non-breaking spaces from a ticketing system and line breaks pasted into a DNS control panel all corrupt terms in ways that are hard to see. Linting the exact published value, not the value in the change ticket, catches them. Our SPF analyzer shows the parsed terms and the lookup count, and the DNS lookup tool shows the raw TXT strings.
How does SPF syntax affect DMARC?
DMARC uses the SPF result for the MAIL FROM domain and requires that domain to align with the visible From domain. Under RFC 9989, which obsoletes RFC 7489, a permerror caused by a syntax mistake or a blown lookup limit means no SPF pass, leaving DKIM as the only route to a DMARC pass. On a domain at p=quarantine or p=reject, that is the difference between delivered and blocked when DKIM also breaks.
Aggregate reports defined in RFC 9990 carry the SPF result per source, including permerror and temperror, so a syntax regression shows up as a sudden shift across every source for that domain. Our guide on why DMARC fails when SPF passes covers the alignment side.
What should an MSP check before publishing an SPF change?
Treat every SPF edit as a change with a rollback, because one stray space affects all of a client's mail. Before publishing, confirm four things:
- Exactly one TXT record on the name starts with
v=spf1, and it parses without errors. - There is one
allat the end, or aredirect=and noall. - The expanded lookup count, including nested includes, is below 10 with headroom for the next sender.
- No
ptr, no+all, and every include target resolves to an SPF record.
For a portfolio, record the published value and lookup count per client domain at onboarding, so that a provider changing its include later is visible as a difference rather than a mystery. Vendor-specific records such as the Google Workspace SPF record are worth re-counting at each audit for exactly that reason. Broader policy choices live in SPF record best practices, and the full publishing procedure, from sender inventory to test messages, is in our guide to creating an SPF record.
FAQ
What is the correct syntax for an SPF include?
Write include: immediately followed by the domain, with no space: include:_spf.example.net. The domain is mandatory. An include counts as one DNS lookup plus every lookup inside the included record, and it only matches when that record returns pass. If the included domain has no SPF record, the result is permerror.
Is SPF syntax case-sensitive?
No. RFC 7208 Section 4.6.1 states that mechanism and modifier names are case-insensitive, following the ABNF rules of RFC 5234, so INCLUDE: and include: behave identically. Domain names in DNS are case-insensitive as well. Lowercase is still the convention, and it makes records easier to compare across a portfolio of client domains.
Do ip4 and ip6 mechanisms count toward the 10 lookup limit?
No. ip4, ip6 and all need no DNS query during evaluation, and neither does the exp modifier, so RFC 7208 Section 4.6.4 exempts them from the limit. Only include, a, mx, ptr, exists and redirect count, including those nested inside included records. Replacing an include with address ranges saves lookups but needs maintenance.
Can I use both redirect and include in one record?
Yes. Includes are evaluated left to right like any mechanism, and the redirect applies only if none of them matched. Both count toward the 10 lookup limit. The one combination that does not work is redirect= with an all mechanism: Section 5.1 says the redirect must be ignored, so the target's policy never applies.
Why should I not use the ptr mechanism?
RFC 7208 Section 5.5 says ptr "SHOULD NOT be published". It is slow, depends on reverse DNS that the IP owner rather than you controls, places load on the .arpa name servers and still counts toward the 10 lookup limit. Receivers must still support it. Use ip4, ip6 or include instead.