Skip to main content

SPF, DKIM and DMARC

What Is an SPF Record? Syntax, Mechanisms, and -all vs ~all

By Tom Sather•Published •8 min read

An SPF record is a DNS TXT record listing the servers allowed to send mail for your domain. Here's how the syntax works and when to use -all or ~all.

On this page

An SPF (Sender Policy Framework) record is a DNS TXT record that lists the servers allowed to send email for your domain. When a message arrives, the receiving server looks up the SPF record for the domain in the envelope sender (the Return-Path) and checks whether the sending IP is on the list. The record ends with an all mechanism that says what to conclude when it isn't: -all means fail, ~all means softfail.

A minimal record looks like this:

v=spf1 include:spf.protection.outlook.com ip4:203.0.113.7 -all

That says: Microsoft 365's servers and 203.0.113.7 may send for this domain, and anything else should fail.

What SPF actually checks

This is the part most people get wrong. SPF doesn't check the From address you see in your mail client. It checks the domain in the SMTP MAIL FROM command, also called the envelope sender, Return-Path or bounce address. RFC 7208 also allows receivers to check the HELO name the sending server announces, but MAIL FROM is the one that matters in practice.

So if you send through an email service provider that uses its own bounce domain, the receiver checks the provider's SPF record, not yours. Your SPF record only comes into play for mail whose Return-Path is your domain or a subdomain you've set it to.

That's also why SPF alone doesn't stop someone spoofing your From address. DMARC fixes that by requiring the SPF-checked domain to align with the From domain. See what DMARC is and how SPF, DKIM and DMARC work together.

Anatomy of an SPF record

Every record starts with v=spf1 and is published as a TXT record. (There used to be a separate SPF record type in DNS; RFC 7208 discontinued it.) After the version come mechanisms, evaluated left to right. The first one that matches the sending IP decides the result, and evaluation stops.

Mechanisms

MechanismWhat it matchesCosts a DNS lookup?
ip4:203.0.113.0/24That IPv4 address or rangeNo
ip6:2001:db8::/32That IPv6 address or rangeNo
aThe IPs of the domain's A/AAAA recordsYes
mxThe IPs of the domain's MX hostsYes
include:example.netWhatever example.net's SPF record passesYes, plus its own lookups
exists:Any domain that resolves, used with macrosYes
ptrReverse DNS of the senderYes, and RFC 7208 says it SHOULD NOT be published
allEverythingNo

a and mx can take a domain (a:mail.example.com) and a prefix length. Without one, they use the current domain.

Qualifiers

Each mechanism can carry a qualifier that sets the result when it matches:

QualifierResultMeaning
+ (default)passAuthorized
-failNot authorized
~softfailProbably not authorized
?neutralNo assertion either way

The qualifier defaults to +, so include:example.net and +include:example.net are the same thing. In practice, you only ever see a qualifier on all.

Modifiers

  • redirect=example.net hands the whole evaluation to another domain's record. It only applies if nothing else matched. Useful when several domains share one policy.
  • exp= points to an explanation string returned with a fail. Rarely used.

The possible SPF results

RFC 7208 defines seven:

  • pass: the IP is authorized.
  • fail: the domain says the IP is not authorized (matched -all or another - mechanism).
  • softfail: the domain says the IP is probably not authorized (~all).
  • neutral: the domain makes no assertion (?all, or no mechanism matched and there's no all).
  • none: no SPF record was found, or no domain to check.
  • temperror: a transient error, usually a DNS timeout.
  • permerror: the record couldn't be interpreted, for example two SPF records, a syntax error, or more than 10 DNS lookups.

What each one does to your mail, and how to fix the error states, is covered in SPF fail, softfail, permerror and temperror.

-all vs ~all

This is the question everyone asks, and the honest answer is that it matters less than it used to, because DMARC now does most of the enforcement.

What the spec says. RFC 7208 says receivers SHOULD NOT reject a message based solely on a softfail, though they may scrutinize it more closely. A fail is a clear statement that the sender isn't authorized, and a receiver rejecting during the SMTP conversation uses a 550 code. What a receiver actually does with either result is its own policy decision.

What happens with DMARC. Under DMARC, softfail and fail are both just "SPF didn't pass." Neither one counts toward DMARC, and the message is judged on DKIM and your DMARC policy. So for a domain with DKIM and an enforced DMARC policy, the difference mostly disappears.

Microsoft's recommendation. Microsoft recommends -all for Microsoft 365 domains, on the grounds that DKIM and DMARC should be in place anyway. It also notes that the DMARC policy is effectively ignored for ~all failures when a message has no DKIM signature, and recommends -all so DMARC can act on those messages.

The case for ~all. Some receivers do reject outright on -all before DMARC is considered. Gmail's 5.7.26 bounce has a version specifically for SPF hard fails (see 550 5.7.26). If your record might be missing a legitimate sender, or your mail is commonly forwarded, -all turns that gap into bounces.

A reasonable default: use ~all while you're still discovering all your sending sources, and move to -all once DMARC reports show every legitimate source passing. Never use +all, which authorizes the entire internet to send as you. Microsoft says ?all is for testing and shouldn't be used in production.

Rules that trip people up

  • One SPF record per domain. Two TXT records starting with v=spf1 on the same name is a permerror. Merge them.
  • 10 DNS lookups, maximum. Every include, a, mx, ptr, exists and redirect counts, including the ones nested inside includes. Go over and SPF returns permerror for everything. See the SPF 10 DNS lookup limit, and count yours with the SPF analyzer.
  • Subdomains don't inherit. The record on example.com doesn't cover news.example.com. Each name that appears in a Return-Path needs its own record.
  • SPF breaks on forwarding. When a mailbox forwards your message, it arrives from the forwarder's IP, which isn't in your record. That's expected. DKIM is what survives forwarding.

How to write one

  1. List every service that sends mail using your domain in the Return-Path. Your mailbox provider, your ESP if it uses a custom bounce domain, your CRM, helpdesk, billing system and any servers you run.
  2. Get each vendor's documented SPF include or IP ranges.
  3. Put it together: v=spf1, then IPs and includes, then ~all or -all.
  4. Count the lookups before you publish.
  5. Publish it as a single TXT record on the domain, then check it with the SPF, DKIM and DMARC checker.
  6. Confirm with a real message. Send a test and look for spf=pass and the domain you expect in the Authentication-Results header. The header analyzer shows this clearly.

For domains that never send mail, publish v=spf1 -all. That tells receivers no server is authorized, which makes the domain less useful to spoofers. Pair it with a DMARC record at p=reject.

When to get help

If your SPF record has grown past 10 lookups and you're not sure which includes are safe to remove, or you can't tell which services actually use your domain in the Return-Path, a quick review beats breaking authentication for the whole domain.

FAQ

Is SPF required?

Effectively, yes. Google requires every sender to authenticate with SPF or DKIM, and bulk senders to Gmail need both. Most other major providers expect it too.

Can I have two SPF records?

No. Two SPF records on the same domain is a permerror, which means SPF fails. Combine them into one record.

Do I need SPF if I have DKIM?

Yes. DMARC passes with either one, but SPF is still a requirement at Gmail for bulk senders and gives you a second aligned pass when DKIM breaks.

Does SPF protect my From address from spoofing?

Not on its own. SPF checks the Return-Path domain, which recipients don't see. DMARC is what ties authentication to the visible From address.

Sources

In this section

Related guides

Still stuck?

Some deliverability problems take an experienced eye to untangle. Tell me what you're seeing and I'll tell you what I'd check first. The first 30-minute call is free.