FortWatch.ai

SPF Explained: The Complete Guide to SPF Records

FortWatch

FortWatch Team

• 26 min read

SPF Explained: The Complete Guide to SPF Records

SPF is a DNS TXT record that says which servers may use your domain in the envelope sender and the HELO greeting of a mail conversation. A receiving server that checks SPF normally does so while the message is still arriving: it compares the connecting server's address with your record and gets one of seven results. You can check any domain's SPF record now with our free checker, and read along with your own record in front of you.

This guide covers what the record is, how a receiving server evaluates it, every mechanism and qualifier, the limit of 10 DNS lookups, what SPF does not protect, and how it fits with DKIM and DMARC. Every statement about how SPF works links to the section of RFC 7208, the standard that defines it, so you can check it at the source.

The short version: publish one SPF record on each domain your mail uses in the envelope sender or the HELO greeting. Start it with v=spf1, list the servers that send with that name, and end it with ~all or -all; the section on qualifiers explains the trade-off. Keep it within the 10-lookup limit. SPF does not check the From address people see, and forwarding can stop it passing, so pair it with DKIM and DMARC.

What an SPF record is

An SPF record is a DNS TXT record that declares which hosts are, and are not, authorized to use a domain name in two places of the mail conversation: the HELO greeting and the MAIL FROM address. It sits on the name it covers and starts with the version tag v=spf1. A typical record looks like this:

example.com.  TXT  "v=spf1 ip4:192.0.2.10 include:_spf.google.com -all"

Read left to right: ip4 authorizes the server at 192.0.2.10, include authorizes any server for which Google's own SPF check passes, and if neither matches and no error stops the check, -all gives fail.

The rules for publishing it

  • TXT only. SPF records must be published as TXT records. A separate SPF record type existed during SPF's experimental phase and is no longer used.
  • One per name. A name must not have more than one SPF record. When a receiver finds more than one SPF record, the check ends in permerror before either is evaluated. Adding a second record for a new service breaks SPF this way; add the service to the one record instead.
  • On the name it covers. The record is placed at the name it pertains to, not on a parent or a child. The record on example.com says nothing about news.example.com, as Microsoft's setup guide also spells out for its customers' subdomains.
  • Long records are split, not doubled. A single TXT string holds at most 255 characters, so a longer record is written as several strings in one record, which receivers join without adding spaces. Put the space inside a string, before the break, or two terms run together.
  • Keep it small. The standard advises keeping the answer to an SPF query within 512 bytes, and in practice the name and all its TXT records under 450, because an oversized answer may be silently ignored.

Which address SPF checks

Keep two sender addresses apart. The envelope sender, given in the SMTP MAIL FROM command, is where bounces go and can be empty, and the receiving server records it as the Return-Path header when it delivers the message. The From header is the address your recipient sees. SPF checks the first one. An SPF check must look at the MAIL FROM address unless a check of the HELO name has already given a definite answer, and the standard recommends checking the HELO name as well, and first.

When a message has an empty envelope sender, as bounce messages do, SPF checks postmaster@ the HELO name instead. That is why the standard advises an SPF record for each host name your mail servers greet with, such as "v=spf1 a -all" on relay.example.com.

This difference matters in practice. A newsletter or ticketing service may send your mail with its own domain in the envelope sender; then SPF checks the service's record, not yours, and passes or fails on the service's behalf. The From address your customers see is never part of the SPF check. The standard says so plainly: a sender can pass SPF with its own domain and still put other identities in the headers. DMARC is what ties the two together, further down.

How a receiving server evaluates the record

The standard describes the check as a function, check_host(), that takes the connecting server's IP address, the domain to look up and the sender address. A receiver may implement it differently, as long as it gets the same results. In order:

  1. If the domain is malformed or does not exist, the result is none.
  2. The receiver asks DNS for the domain's TXT records. A DNS server failure or a timeout ends the check with temperror.
  3. It keeps the records whose version is v=spf1 followed by a space or the end of the record, so v=spf10 does not count. None left means none; more than one means permerror.
  4. It checks the syntax of the whole record first. A syntax error anywhere means permerror, before anything is evaluated.
  5. It tests the mechanisms from left to right. The first one that matches ends the check, and its qualifier gives the result; an error also ends it, with the error as the result.
  6. If nothing matches and the record has a redirect, the check continues with the record it names. If nothing matches and there is no redirect, the result is neutral, as if the record ended in ?all.

The seven results

ResultWhat it meansWhat the standard says about handling it
passThe server is authorized to use the domainThe domain can be held responsible for the message
failThe server is explicitly not authorizedLocal policy; a rejection should use 550, with 5.7.1 where supported
softfailThe server is probably not authorizedReceivers should not reject on this alone, but may look closer
neutralThe domain makes no statement about the serverTreated exactly like none
noneNo SPF record, or no usable domain to checkThe receiver has no information
temperrorA temporary error, usually in DNS; a retry may workAccept, or reject for now; a rejection should use 451, with 4.4.3 where supported
permerrorThe published records cannot be interpreted; the DNS owner has to step inA rejection should use 550, with 5.5.2 where supported

Note what the last column does not say. Apart from treating neutral like none, SPF leaves the decision to the receiver: some receivers reject on fail, some only record it in a header, and some weigh it with other signals. Your record states a policy; the receiver decides what to do with it. A receiver that accepts a message is asked to record the result in a Received-SPF or Authentication-Results header, which is where you read it when you test.

The mechanisms, one by one

A record is a list of terms. Most are mechanisms, which test the connecting server and match, do not match, or return an error. Each can carry a qualifier in front, covered in the next section. The table sums up the mechanism definitions and which ones count toward the 10-lookup limit.

MechanismMatches whenCounts toward the 10-lookup limit
allAlways, if the check gets that farNo
ip4:, ip6:The server's address is in the given address or rangeNo
aThe server's address is one of the domain's A or AAAA addressesYes
mxThe server is one of the domain's MX hostsYes
include:Another domain's SPF record passes the serverYes, plus what that record counts
exists:A name built for this message has an A recordYes
ptrA reverse DNS name of the server points back to its address and is in the domain; do not useYes

all

all always matches, so it belongs at the end and decides every server nothing before it matched. Mechanisms after it are never evaluated, and a redirect anywhere in the record is ignored. The whole record still has to pass the syntax check, and an exp modifier can still supply the explanation for a fail.

ip4 and ip6

ip4:192.0.2.10 matches one address and ip4:192.0.2.0/24 a range. Without a prefix length, ip4 means /32 and ip6 means /128, a single address. You may not shorten an address instead of giving a prefix: 192.0.2 is not a valid range. These two cost no DNS lookup, which makes them the cheapest way to list a server whose address does not change.

a and mx

a matches the domain's own addresses, A records for IPv4 and AAAA for IPv6; a:other.example.com uses another name's. mx looks up the domain's MX records and then the addresses of each mail host. Both accept a prefix, such as a/24. They are handy for small setups where your own mail server also sends, and they follow your DNS when an address changes. Each counts once toward the 10-lookup limit, though mx can take several DNS queries behind that one count. The standard's own advice is to prefer ip4 and ip6 over a, and a over mx, unless addresses change often.

include

include:_spf.google.com runs a full SPF check against Google's record and matches only if that check passes. This is how you authorize a provider: the provider keeps the list of its servers, and your record points to it. Two details catch people out:

  • The name is misleading. The other record is not pasted into yours; only its result is used. A -all at the end of the included record does not end your check, it just makes the include not match, and your record carries on with its next term. The standard itself says "if-match" would have been a better name.
  • If the check reaches an include whose name has no SPF record, the result is permerror, not "no match". Mail can still pass on an earlier term, so an include left behind for a service whose record is gone can break some of your mail while the rest keeps working.

exists

exists: builds a domain name, usually from parts of the message using macros, and matches if that name has any A record. It lets a provider answer per sender or per address from its own DNS. You will mostly meet it inside a provider's record rather than write it yourself.

ptr

ptr looks up the server's reverse DNS name, checks that the name points back to the server's address, and matches if it is the domain or a subdomain of it. The standard says it should not be published: it is slow, less reliable when DNS fails, and loads the reverse-DNS servers. SPF software must still support it, but replace it with ip4, ip6 or an include that covers the same senders.

The two modifiers: redirect and exp

Modifiers are written name=value and do not match anything themselves. redirect=_spf.example.com hands the whole check to another record, but only if nothing in yours matched and only if yours has no all. The other record's result becomes yours, and if it has no SPF record the result is permerror. It is meant for several domains of one organization sharing one record; for a third-party provider, include is the right tool.

exp= names a TXT record holding an explanation a receiver can return on fail, such as a link to your mail policy. Each of the two may appear at most once, or the record is a permerror. A modifier the receiver does not know is ignored.

Macros, briefly

Names in include, exists, redirect and others can contain macros such as %{i} for the server's IP address or %{d} for the domain being checked, filled in for each message. They power provider-side schemes like per-user policies. A simple record you write by hand needs none. A macro that breaks the macro grammar is a syntax error, and a valid macro that points at the wrong name can give the wrong result.

Qualifiers: what -all, ~all, ?all and +all say

Any mechanism can start with a qualifier. When the mechanism matches, the qualifier becomes the result:

QualifierResultAt the end of a record
+ (the default when none is written)pass+all passes every server that gets that far
-fail-all: servers not listed are not authorized
~softfail~all: servers not listed are probably not authorized
?neutral?all: no statement about servers not listed

The qualifier on the final all is the record's policy for everyone you did not list, and it is where the real choice is:

-all or ~all?

Sources you would expect to agree do not. Google's setup guide gives v=spf1 include:_spf.google.com ~all for a domain that sends only through Google Workspace. Microsoft recommends -all for Microsoft 365 domains, together with DKIM and DMARC. The current DMARC standard adds a warning the other way: some receivers check SPF before DMARC, so -all can get a message rejected before DMARC runs, even one that a valid, aligned DKIM signature would have passed, and such rejections never appear in your DMARC reports.

A workable path: start with ~all, list every service that sends as you, and publish DMARC with reports. Consider -all once you have checked every legitimate sending path and accept the risk to forwarded mail. Clean DMARC reports cannot prove the switch is safe, because mail rejected early never shows up in them. Either ending is far better than ?all or +all, which are the two FortWatch raises an issue for.

The 10-lookup limit, and what counts toward it

Checking SPF costs the receiver DNS queries, and an include can lead to another record with more includes. To stop that from growing without end, the standard caps it: one check may evaluate at most 10 terms of these kinds, the include, a, mx, ptr and exists mechanisms and the redirect modifier, nested records included. Reaching an 11th gives permerror. The cap counts terms, not DNS queries. all, ip4 and ip6 do not count; nor does exp, though fetching its explanation can take a DNS query later.

The limit covers the whole check, not each record. It is tracked as one count across every nested record, so the includes inside your providers' records count against your 10. That is why the number of includes you can see is a poor guide. Microsoft makes the same point: fewer than 10 include: statements doesn't guarantee fewer than 10 DNS lookups.

A worked example:

v=spf1 ip4:192.0.2.10 mx include:_spf.google.com include:spf.protection.outlook.com -all
  • ip4:192.0.2.10: no lookup.
  • mx: one.
  • Each include: one, plus the counted terms its own record reaches in turn. You do not control that part, and a provider can change it without telling you.
  • -all: no lookup.

Three smaller limits sit beside the main one:

  • MX hosts. Each mx has a separate limit of 10 address lookups (A or AAAA) for the mail hosts it finds; going over gives permerror.
  • Void lookups. A lookup that finds nothing is a "void lookup", and receivers should allow only two per check before returning permerror. a or mx terms that point at names which no longer exist use these up.
  • Time. Receivers should allow at least 20 seconds for a check; one that takes longer may end in temperror.

Because evaluation stops at the first match, a server listed early may pass well before the 10th lookup, while a server further down, or a forger who matches nothing, takes the full path. A record over the limit can therefore work for some of your mail and fail for the rest, which makes the fault hard to spot from complaints alone. So count the longest path a check can actually take, nested records included. A receiver stops at the first match or error, so adding up every term of every nested record can overstate the count.

The usual ways back under the limit, in short: remove includes you no longer need; list fixed servers with ip4 or ip6; or give a bulk sender a subdomain of its own as its envelope domain, with its own SPF record, which Microsoft also recommends for services outside your direct control. Take its include out of the main record only once it no longer sends with the main domain in the envelope. Copying a provider's current addresses into your record, often called flattening, saves lookups but goes stale the day the provider changes them.

What SPF does not protect

The From address people read

SPF checks the envelope sender and the HELO name. It says nothing about the From header in the recipient's mail client, and the standard warns that a sender can pass SPF with its own domain while showing other identities in the headers. A phishing message from a domain the attacker owns, with your company's address in the From line, can pass SPF perfectly. Closing that gap is DMARC's job.

Forwarded mail

SPF checks the server that hands the message over. When a mailbox forwards a message, the receiver sees the forwarder's address. If the forward keeps your envelope sender and your record does not list the forwarder, SPF will not pass. The standard describes fixes for senders, forwarders and receivers, but no change to your record covers every forwarding service. When a forwarder does rewrite the envelope sender, SPF may pass, but for the forwarder's domain, not yours. DKIM is what can carry your mail through: when a forward leaves the message unchanged, DKIM signatures may remain valid.

A pass is not a sign of trust

A pass says only that the server was allowed to use that domain. The standard points out that it can return pass for mail that is harmful, from spammers who set up SPF on throwaway domains, and fail for mail that is legitimate, such as mail that went through an alias.

Names without a record

Each record covers only its own name. A subdomain that sends mail needs its own record, and a domain that sends no mail at all should say so with the record the standard gives for it, v=spf1 -all, so nobody can pass SPF as it. For the many names under a domain that never send, Microsoft notes that protection for undefined subdomains is best handled by DMARC.

How SPF fits with DKIM and DMARC

The three records do different jobs. SPF lists the servers allowed to send with your domain in the envelope. DKIM signs each message, so a receiver can check that the signed parts were not changed and which domain signed them; you can check a DKIM key the same way as SPF. DMARC ties the two to the From address people actually see.

For DMARC, an SPF pass counts only if the envelope sender's domain is aligned with the From domain: the same domain, or in relaxed mode, which is the usual choice, the same organizational domain. The standard explains why: any domain owner, including an attacker, can publish SPF for its own domain and get a pass. So when a service sends your mail with its own bounce domain, SPF passes for the service and does nothing for your DMARC. If the service lets you set a custom bounce or return-path domain, publish SPF on that name as the provider instructs and check that it passes with alignment. In relaxed mode a return-path such as bounce.example.com aligns with example.com in the From line; strict mode needs the exact domain.

Where a DMARC policy applies, a message passes DMARC when SPF or DKIM passes with alignment. Mailbox providers have made at least one of them a condition of delivery:

Our DMARC guide covers the policy, the reports and the path from p=none to p=reject.

Setting up SPF, step by step

  1. List every service that sends as your domain. Your mailbox provider, your website's contact form, the CRM, the newsletter tool, the help desk, invoicing, and any server that sends alerts. Google's guide makes identifying every sender its first step. For each one, find which domain it uses in the envelope sender: that domain's record is the one that has to list it.

  2. Find what each service asks you to publish. Each provider documents its own value, such as include:_spf.google.com for Google Workspace and include:spf.protection.outlook.com for Microsoft 365. For your own servers, use ip4 and ip6. Check which domain the service uses in the envelope sender. If it uses its own, its own SPF record applies and your include does nothing for that mail. If you set up a custom return-path, follow the provider's DNS instructions for that exact name; an include on your main domain does not cover a return-path subdomain.

  3. Write one record. Start with v=spf1, put the cheap terms (ip4, ip6) first and the includes after, and end with ~all, or with -all once you have weighed the trade-off above.

  4. Publish it as a TXT record on the name used in the envelope sender or HELO greeting. If a record already exists, edit it; do not add a second one. A subdomain that sends gets its own record.

  5. Lock down the names that send nothing. For a name never used in an envelope sender or HELO greeting, such as a parked domain or one used only for a website, the standard recommends v=spf1 -all.

  6. Check it. Run the SPF checker for the syntax, the policy and the lookup count, then send a test message to a mailbox you control and read the Received-SPF or Authentication-Results header.

  7. Change it with care later. Mail can still be in transit when you edit the record, so the standard advises a transition period in which the old policy stays valid. Add a new sender before you start using it, and remove an old one a while after it stops.

  8. Add DMARC. Start with p=none and a rua address for reports, and use the reports to fix authentication and alignment before you enforce.

How to check your record

From a terminal, dig +short TXT example.com on Linux or macOS, or nslookup -type=TXT example.com on Windows, lists the TXT records on a name; our guide to reading DNS records covers the output. Look for exactly one that starts with v=spf1. That shows the record, but not what it costs: the lookups hide inside the includes.

The free FortWatch SPF checker reads the record from your browser through Cloudflare's public DNS, so the domain you check never reaches our servers. It then:

  • checks the record's syntax and flags a name with more than one SPF record, either of which fails the record outright;
  • finds the policy at the end of the record, following a redirect when there is no all;
  • follows the includes before the first all, and a redirect when the record has no all, through nested records too, to estimate the lookups for a sender that no term matches; it warns at 9 and 10 and fails above 10;
  • shows what each include costs, so you can see which one to cut;
  • flags an include it follows whose name has no SPF record, an include loop, ptr, and mechanisms after the first all, which are never evaluated.

It does not check void lookups or the separate MX and PTR address limits, and it does not test a real message. It says when its count is incomplete: a followed include or redirect that uses a macro, a DNS lookup that failed, or its own limit of 10 nested levels or 40 records.

A record checked once can change later: a new service gets added as a second record, or someone edits the ending during a migration. FortWatch's DNS and email monitoring checks SPF on the names you add with email checks on. They are on by default for two-part names such as example.com and their www names; for www.example.com it reads the record of example.com. Other names, example.co.uk included, are checked once you switch email checks on for them. Scans run weekly on Essential, and Pro and above can also scan daily. A missing record is a high issue when the name has MX records and medium when it does not; a record containing +all is high, and otherwise one containing ?all is medium. It does not follow includes, check the syntax or count lookups; that is the free checker's job.

Common questions

Do I need SPF if all my mail goes through Google Workspace or Microsoft 365?

Yes. Check that the domain in your envelope sender has an SPF record that lists your provider. For your own domain that usually means your DNS settings, though Google's guide notes that SPF may already be set up by default, for example on a domain bought through a Google partner. Microsoft maintains the records of its own onmicrosoft.com domains. Gmail requires SPF or DKIM from everyone sending to personal Gmail accounts.

Can a domain have two SPF records?

No. A receiver that finds two SPF records on one name returns permerror and evaluates neither. Merge them into one record with every sender in it.

Does my SPF record cover my subdomains?

No. A record covers only the name it sits on. A subdomain that sends mail needs its own record; one that sends nothing can publish v=spf1 -all.

Should my record end with -all or ~all?

Both are reasonable, and the sources differ: Google's example uses ~all, Microsoft recommends -all, and the DMARC standard warns that -all can get mail rejected before DMARC is checked. Start with ~all. Consider -all after checking your sending paths and the risk to forwarded mail; clean DMARC reports alone do not prove it is safe. Never ?all or +all.

Why does SPF fail when mail is forwarded?

Because the receiver checks the server that delivered the message, and after a forward that is the forwarder's. If the forwarder keeps your envelope sender and your record does not list it, SPF does not pass, as the standard explains in its section on forwarders and mailing lists. An aligned DKIM signature can still carry DMARC through if it survives the forward; a change to the signed content can break it.

What does "permerror" mean, and how do I fix it?

It means the published records could not be interpreted, and the DNS owner needs to look into it. Common causes are two SPF records on one name, a syntax error, going over a lookup limit, and an include of a name with no SPF record. The checker catches most of these, but its count can be incomplete, and it does not check void lookups or the separate MX address limit.

Does SPF stop people from putting my address in the From line?

Not on its own. SPF checks the envelope sender, which an attacker can set to a domain they own; the From line is not part of the check. DMARC with an enforcing policy is what lets receivers act on mail that uses your domain in the From line without passing SPF or DKIM for it.

What do I do with this?

  1. Check your record now. Run your domain through the SPF checker: one record, valid syntax, an ending of ~all or -all, and 10 lookups or fewer.

  2. Compare the list with reality. Authorize every service that uses this domain in its envelope sender, and remove the ones that no longer do. A service that uses its own envelope domain is covered by its own record.

  3. Cover every name. Each sending subdomain gets its own record; each domain or subdomain that sends nothing gets v=spf1 -all.

  4. Make SPF count for DMARC. Where a service supports it, set a custom return-path under your domain, publish SPF on that name, and check that it aligns with your From domain. Set up aligned DKIM signing too.

  5. Publish DMARC with reports, as the DMARC guide walks through, and use them to find missing senders and alignment problems.

  6. Keep watching it. Records change when people add services. FortWatch rechecks the domains with email checks on, on your scan schedule, and raises an issue for a missing SPF or DMARC record, an SPF record containing +all or ?all, and a DMARC policy of none. For the rest of your DNS, the DNS security checklist covers DKIM, DMARC, DNSSEC and CAA.

Share this post

Ready to secure your stack?

Secure your entire stack today

Start scanning in under 5 minutes. No credit card required. 7-day free trial included.