Email

SPF Explained

Learn how SPF records list who may send for your domain, how to write one correctly and why it fails on forwarded mail.

Key takeaways

  • SPF is a TXT record that names the IP addresses and services allowed to send for a domain.
  • It checks the envelope sender or greeting name used between servers, which readers normally don't see.
  • A domain must have only one SPF record, and evaluating it may trigger at most ten DNS lookups.
  • Forwarding often breaks SPF, which is one reason to use DKIM and DMARC alongside it.
  • Ending with ~all or -all tells receivers how to treat senders you did not list.

SPF, the Sender Policy Framework, is a DNS record that lists which servers are allowed to send email using your domain. When a message arrives, the receiving server looks up that list and checks whether the connecting IP address is on it. The standard is RFC 7208.

If you own a domain, you should publish one correct SPF record that covers every service that sends mail for you. It is a short line of text, but a few rules about it are strict, and breaking them silently makes the whole check fail.

What SPF checks, and what it doesn't

During an SMTP session the sending server states an envelope sender with MAIL FROM and introduces itself with EHLO. SPF evaluates the domain in the envelope sender, and when that is empty, as in bounces, the greeting name.

It does not check the From address you see in your mail app. A message can pass SPF for the spammer's own domain while showing your brand in the visible From line. Closing that gap is the job of DMARC, which requires the domain that passed to match the one displayed. Email spoofing explains why the two addresses can differ.

SPF also says nothing about content, encryption or whether a message is wanted.

An SPF check at delivery
Sending server Receiving server DNS MAIL FROM:<[email protected] > 1 TXT record for example.com? 2 v=spf1 ip4:192.0.2.0/24 -all 3 Continue, flag or refuse 4 After comparing the connecting IP with the list Sending server Receiving server DNS MAIL FROM:<new s@example. com> 1 TXT record for example.co m? 2 v=spf1 ip4:192.0.2. 0/24 -all 3 Continue, flag or refuse 4 After comparing the connecting IP with the list

Anatomy of a record

An SPF record is a TXT record at the domain it protects. It begins with the version tag and then lists terms, evaluated from left to right until one matches.

example.com.  3600  IN  TXT  "v=spf1 mx ip4:192.0.2.10 include:_spf.example.net -all"
Reading an SPF record
v=spf1 1 mx 2 ip4:192.0.2.10 3 include:_spf.example.net 4 -all 5 Version Identifies the TXT record as SPF. 1 MX hosts Allows the addresses of the domain's own mail servers. 2 Address Allows this IPv4 address or range. 3 Delegation Allows whatever that domain's SPF record allows. 4 Everyone else Any other source should fail. 5 v=spf1 1 mx 2 ip4:192.0.2.10 3 include:_spf.example.net 4 -all 5 Version Identifies the TXT record as SPF. 1 MX hosts Allows the addresses of the domain's own mail servers. 2 Address Allows this IPv4 address or range. 3 Delegation Allows whatever that domain's SPF record allows. 4 Everyone else Any other source should fail. 5

Mechanisms

Mechanism Matches
ip4: and ip6: The listed address or CIDR block
a The address records of the domain, or of a named one
mx The addresses of the domain's MX hosts
include: A pass from another domain's SPF record
exists: Whether a constructed name resolves, for advanced setups
ptr Reverse DNS match; the standard advises against it
all Everything; always put it last

The redirect= modifier is different: it hands the whole evaluation to another domain's record, and is useful when many domains share one policy. It is ignored if the record contains all.

If CIDR notation like /24 is unfamiliar, the subnet calculator shows which addresses a block covers.

Qualifiers

Each mechanism can carry a prefix that decides the result when it matches:

  • + pass (the default if nothing is written)
  • - fail: not authorized
  • ~ softfail: probably not authorized
  • ? neutral: no statement

So -all asks receivers to treat unlisted senders as failing, and ~all marks them as suspect. Many domains use ~all while they are still discovering their senders, or permanently, letting DMARC make the enforcement decision. +all authorizes the whole internet and defeats the purpose.

Results a receiver can get

Result Meaning
pass The IP is authorized
fail The IP is explicitly not authorized
softfail Weakly not authorized
neutral The domain makes no assertion
none No SPF record found
temperror A temporary problem, such as a DNS timeout
permerror The record is broken

What the receiver does with a result is its own policy. These verdicts appear in the Authentication-Results header, covered in how to read email headers.

The two rules people break

One record only

A domain must not publish two TXT records beginning v=spf1. If it does, the result is a permanent error. This commonly happens when a new service's instructions say "add this record" and someone adds a second one. Merge them instead:

v=spf1 include:_spf.example.net include:mail.example.org ~all

Ten DNS lookups

Evaluating a record may not require more than ten mechanisms or modifiers that cause DNS lookups. include, a, mx, ptr, exists and redirect count, including those nested inside included records. ip4, ip6 and all do not. The standard also limits lookups that return nothing.

Exceed the limit and you get permerror. To stay under it:

  • Remove services you no longer use.
  • Delete redundant a or mx terms.
  • Send some mail streams from subdomains, each with its own record.
  • Be cautious about "flattening" includes into raw IP lists, because providers change their addresses and your copy goes stale unless something updates it.

Long records may also need to be split into several quoted strings inside a single TXT record. Receivers join them without adding spaces. TXT records explains the format.

Publishing SPF step by step

  1. List your senders. Mail provider, web server, contact forms, billing system, help desk, newsletter tool, office devices.
  2. Check whose domain each uses as the envelope sender. Many services use their own bounce domain, in which case your record doesn't affect SPF for them.
  3. Write one record using each provider's documented include or addresses.
  4. Start with ~all.
  5. Publish it at your DNS host, as a TXT record at the domain.
  6. Query it to confirm.
dig example.com TXT +short
Resolve-DnsName example.com -Type TXT
  1. Send test messages from every service and read the headers.
  2. Review DMARC reports to find senders you missed. Reading DMARC reports shows how.

Domains that send no mail

Parked and unused domains are attractive to forgers. Publish a record that authorizes nobody:

example.org.  IN  TXT  "v=spf1 -all"

Pair it with a rejecting DMARC policy. A null MX record (RFC 7505) can also state that the domain accepts no mail. Subdomains do not inherit a parent's SPF record, so any subdomain that sends mail needs its own.

Third-party senders and subdomains

Outside services are where SPF gets complicated. There are three common arrangements, and each asks something different of you.

Arrangement Envelope sender domain What you publish
Service uses its own bounce domain Theirs Nothing for SPF; rely on DKIM for alignment
Service uses a custom bounce subdomain of yours Such as bounce.example.com The records they specify on that subdomain
Service sends with your main domain example.com Add their include to your main record

The middle arrangement is often the tidiest. The subdomain gets its own SPF record, usually through a CNAME or a TXT record pointing at the service, so it uses none of your main record's lookup budget. Because it shares your organizational domain, it can also satisfy DMARC under relaxed alignment.

Separating mail streams this way also contains mistakes. If a marketing platform's setup changes, your staff mail is unaffected.

Worked example: counting lookups

Say your record is:

v=spf1 a mx include:_spf.example.net include:mail.example.org ip4:192.0.2.10 -all

Start counting. a is one lookup and mx is another. Each include is one, plus whatever lies inside it. Suppose _spf.example.net contains three further includes: that include costs four in total. Suppose mail.example.org contains only IP ranges: it costs one. The ip4 and -all terms cost nothing. The total is 2 + 4 + 1 = 7, which leaves room for three more.

Now imagine a new help desk service whose include nests three others. That adds four and brings you to eleven, so the whole record starts returning permerror, including for senders that worked yesterday. Removing the unneeded a and mx terms would bring it to nine. Recount whenever you add a service, and remember that a provider can change what is inside its include without telling you.

Maintaining the record

SPF is easy to publish and easy to forget. Review it when you adopt or drop a service, move web hosts or change office internet connections. Keep a short note of why each term is there and who owns it. When editing, lower the record's TTL ahead of time if you want a mistake to clear quickly, and test immediately afterward. A single typo, such as include= in place of include:, can invalidate the whole evaluation.

Why forwarding breaks it

When a mailbox forwards your message elsewhere, the forwarder's server makes the new connection. Its IP address isn't in your record, so SPF fails at the final destination, unless the forwarder rewrites the envelope sender to its own domain, a technique often called SRS. Rewriting lets SPF pass for the forwarder but no longer matches your From domain.

This is why DKIM matters: its signature travels with the message and can survive forwarding when the content is not changed.

Testing after a change

Don't treat a published record as finished until real mail has passed through it. Send one message from each service to a mailbox at an outside provider, open the raw source and find the SPF result. Note both the verdict and the domain listed after smtp.mailfrom, since a pass for a vendor's domain tells you your own record was never consulted.

Common mistakes

  • Publishing the record on the wrong name, such as www.
  • Forgetting IPv6 addresses for a server that sends over IPv6.
  • Including a provider because it handles incoming mail, though it never sends for you.
  • Authorizing huge address ranges, such as a whole shared cloud network, which lets strangers on it pass.
  • Adding a second record instead of editing the first.
  • Assuming SPF alone stops impersonation.

For the larger picture of why legitimate mail gets filtered, see why your emails land in spam.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Email →

Latest news on this topic