Skip to content

SPF explained, with ready-made records

Section 1 of 11
Size 3 of 5

Professional email05/08Part of the “Professional email for your company” track · Article 5 of 13

SPF explained, with ready-made records

SPF in depth: what it checks, mechanisms and qualifiers, ~all or -all, the 10-lookup limit, one record per name, flattening and forwarding, with examples.

Abdulwahed MohammadContent writer at tech+15 min read

An online shop in Manama sends its order confirmations from its hosting, its newsletter through a mailing service and its staff mail through a mailbox provider. How does a receiving server know which of them is real and which is someone pretending? The first answer is SPF, the Sender Policy Framework, defined in RFC 7208: a single DNS record in which the domain owner lists every server allowed to send in its name. Our article on why emails go to spam introduced it in a line. Here we take it apart: what SPF really checks, how a record is built, every mechanism and qualifier, the limits that break records in practice, flattening, forwarding, and worked records for common setups that you can adapt.

01What SPF checks, and what it does not

Every message carries two sender addresses. The first is the , which the sending server announces in the SMTP command MAIL FROM, and which the receiving server later writes into the Return-Path header; bounces go to it. The second is the From address inside the message, the one the reader sees. They are often the same, but not always: a newsletter service usually puts its own bounce address in the envelope while the From line shows yours.

SPF checks the domain of the envelope sender. The receiving server takes that domain, fetches its SPF record and asks one question: is the IP address connecting to me in this list? When the envelope sender is empty, as it is in bounce messages, the check uses the name the server gave in its HELO greeting instead.

This has a consequence many people miss: SPF on its own does not protect the From address. A fraudster can send from his own server with an envelope on his own domain, pass SPF for that domain, and still write your address in the From line. Linking the two is DMARC’s job: it requires the domain that passed SPF to match the domain in From, as DMARC explained shows.

SPF checks the Return-Path domain, here the newsletter service’s
# The envelope sender, as the receiving server recorded itReturn-Path: <[email protected]># The address the reader seesFrom: News <[email protected]># SPF was checked for mailer.example.com, not for dukkan-isatown.comReceived-SPF: pass (mailer.example.com: 198.51.100.40 is permitted sender)

In this example SPF passes, but for the service’s domain. For DMARC to count the pass for the shop, the service has to put the shop’s own domain in the envelope, usually a subdomain such as bounce.dukkan-isatown.com. Most services offer this as a “custom return path” or “custom bounce domain” setting.

02Anatomy of an SPF record

An SPF record is a TXT record at the domain’s name. It starts with the version, followed by terms separated by spaces:

A complete SPF record
# Name: @ (the domain itself) · Type: TXTv=spf1 mx ip4:203.0.113.25 include:_spf.google.com ~all

The receiving server reads the terms from left to right. Each describes a set of addresses, and the first one that matches the connecting IP ends the check with that mechanism’s result. If nothing matches, the all at the end catches everything else. So this record says: the domain’s MX servers may send, so may 203.0.113.25, so may Google Workspace’s servers, and anything else is probably not authorised. A few rules of form:

  • TXT only: SPF once had its own DNS record type, but RFC 7208 discontinued it. Publish SPF as a TXT record.
  • It starts with v=spf1: Exactly that, followed by a space. A TXT record that does not start with it is not an SPF record, so other TXT records at the same name, such as site verification codes, do no harm.
  • Long records are split, not doubled: One string in a TXT record holds up to 255 characters. A longer record is written as several quoted strings in a single TXT record, and the receiver joins them without adding spaces, so keep the space inside a string.
  • Keep it small: The RFC asks that the DNS answer fit in 512 bytes, so a record that grows past a few hundred characters is a sign to simplify.
A long SPF record split into two strings
# One TXT record holding two strings; note the space at the end of the first"v=spf1 ip4:203.0.113.25 ip4:203.0.113.26 include:_spf.google.com " "include:spf.protection.outlook.com ~all"

03The mechanisms, one by one

MechanismWhat it matchesCounts toward the 10Example
ip4An IPv4 address or rangeNoip4:198.51.100.0/24
ip6An IPv6 address or rangeNoip6:2001:db8:45::/48
aThe addresses of a host name, the domain itself by defaultYes, 1a:mail.example.com
mxThe addresses of the domain’s MX serversYes, 1mx
includeWhatever another domain’s SPF record authorisesYes, 1 plus that record’s owninclude:_spf.google.com
existsAny address, if a name built from macros existsYes, 1exists:%{i}._spf.example.com
ptrThe connecting address’s reverse DNS nameYes, 1Do not use
allEverythingNo~all

Some notes on each:

  • ip4 and ip6: The cheapest mechanisms: they cost no DNS lookups, so they never push you over the limit. Use them for servers you control whose addresses do not change, such as your own VPS. A range in CIDR form covers several addresses: /24 is 256 IPv4 addresses, so make the range no wider than you need.
  • a and mx: Convenient because they follow the DNS: if the server’s address changes, the record keeps working. Both accept a domain and a prefix length, such as a:mail.example.com/28. Remember that mx authorises the servers that receive your mail, which helps only if those same servers also send it.
  • include: The way to authorise another company’s servers: you point to its SPF record and it maintains the list. The name misleads: include does not copy the other record’s all. If the included record does not match, the check simply moves on to your next term. If the included domain has no SPF record, though, the result is permerror.
  • exists: An advanced mechanism that builds a name from macros, such as the connecting IP, and matches if that name has an address record. Most businesses never need it.
  • ptr: Matches by reverse DNS, which is slow and unreliable. RFC 7208 says it should not be published, so leave it out.
  • all: Always matches, so it belongs at the end, where it decides the fate of everything not listed before it.

There are also two modifiers, written with an equals sign. redirect= hands the whole decision to another domain’s record, which helps when several of your domains share one policy; it applies only when no mechanism matched, so a record with redirect should not also end with all. exp= names a TXT record holding an explanation a receiver may show when it rejects; few receivers use it.

Sharing one SPF policy between several domains with redirect
# In each of the company’s other domains · Name: @ · Type: TXTv=spf1 redirect=_spf.dukkan-isatown.com# In dukkan-isatown.com · Name: _spf · Type: TXTv=spf1 ip4:203.0.113.25 include:_spf.google.com -all

04Qualifiers and results: ~all or -all?

Every mechanism can carry a in front of it that sets its result when it matches. Without one, the qualifier is +.

QualifierResultWhat it says
+passThis server is authorised (the default)
-failThis server is not authorised
~softfailProbably not authorised: accept, but treat with suspicion
?neutralNo statement either way

A check can end in seven results in all: the four above; none, when the domain has no SPF record; temperror, when a DNS lookup failed for a moment; and , when the record itself is broken. Receivers usually retry later after a temperror, and treat permerror as if the domain had no working SPF at all; under DMARC it counts as a failure.

The qualifier matters most on all. Which should you end with? The two biggest providers give different advice: Google recommends ~all for Workspace domains, while Microsoft recommends -all for Microsoft 365 domains that also use DKIM and DMARC. Both are reasonable:

  • ~all forgives: A server you forgot to list, or a message forwarded through another server, gets softfail rather than fail, so it is more likely to arrive while you are still finding your senders. Most receivers do not reject on softfail alone.
  • -all is clear: It states that nothing else may send. But some receivers reject a fail during the SMTP conversation, before they read the DKIM signature, so a forwarded message that DKIM would have saved can be lost.

A practical path: start with ~all while you take stock of everything that sends as your domain, add DKIM and DMARC, and once DMARC reports show every legitimate sender passing, decide whether -all adds anything. With DMARC at quarantine or reject, the protection comes from DMARC either way. For a domain that never sends mail, -all is always right.

05The 10-lookup limit and void lookups

Checking SPF costs the receiver DNS queries, and a record that set off hundreds of them could be turned against DNS servers. So RFC 7208 sets hard limits:

  • 10 lookups: The terms that need DNS, which are include, a, mx, ptr, exists and redirect, may be evaluated no more than 10 times in total, counting those inside every included record. ip4, ip6 and all are free. The eleventh turns the result into permerror.
  • 2 void lookups: A lookup that returns no answer, or a name that does not exist, is a void lookup. Receivers should stop at two, with permerror. Old includes for services you no longer use are the usual cause.
  • At most 10 hosts behind mx: An mx mechanism may not lead to more than 10 address lookups, so a domain with more than 10 MX hosts cannot rely on mx.

The trap is that includes nest. include:_spf.google.com is one term in your record, but Google’s record itself includes others, and each of those counts too. This is how a record that looks short adds up:

Counting lookups: each include counts with everything inside it
v=spf1 a mx include:_spf.google.com include:spf.protection.outlook.com include:servers.mailer.example.com include:crm.example.net ~all# a                                    1# mx                                   1# include:_spf.google.com              1 + everything inside it# include:spf.protection.outlook.com   1 + everything inside it# include:servers.mailer.example.com   1 + everything inside it# include:crm.example.net              1 + everything inside it# Six terms in the record, but often more than 10 lookups in all

Note the order, too: receivers stop at the first match, so a record over the limit may still pass for a sender listed early and fail only for one listed late. Count again whenever a provider changes its record, since you cannot see when it does. Ways to stay under 10, from best to worst:

  1. 01Remove what you no longer use: An include for a service you stopped using two years ago still costs a lookup, or a void lookup.
  2. 02Drop a and mx when they add nothing: If your MX hosts belong to your mailbox provider, which is already in an include, mx spends a lookup for nothing.
  3. 03Move senders to subdomains: Each name has its own record and its own limit of 10. Your newsletter can send from news.dukkan-isatown.com with only the newsletter service in its record.
  4. 04Replace an include with ip4 and ip6: Only for a service with a small, documented, stable set of addresses. This is flattening, and it has risks.

06One record per name

A name must not have two SPF records. If it does, the receiver cannot choose between them and the result is permerror, so both stop working. It usually happens when a new service tells you to “add this TXT record” and you add it beside the one you have instead of merging the two.

Merging two SPF records into one
# Wrong: two records at the same name, so the result is permerrorv=spf1 a mx ~allv=spf1 include:servers.mailer.example.com ~all# Right: one record that holds bothv=spf1 a mx include:servers.mailer.example.com ~all

When you merge, keep one v=spf1 at the start and one all at the end, and count the lookups again.

A related rule: SPF records are not inherited. The record at dukkan-isatown.com covers only mail with dukkan-isatown.com in the envelope; news.dukkan-isatown.com needs its own record if it sends. Names that never send are better protected by DMARC, whose policy does reach subdomains.

07Flattening and its risks

replaces include terms with the IP addresses they lead to, so the record costs no lookups. It does solve the 10-lookup problem. But it trades one risk for another:

  • Providers change their addresses: Large services add and retire sending addresses without telling you. Their include always reflects the current list; your copy does not. On the day they start sending from a new range, your mail from it fails SPF.
  • Some providers must never be flattened: Microsoft says plainly not to flatten its include, because its sending addresses are dynamic and change often.
  • The record grows: Dozens of ranges can push the record past the size the RFC asks for, with split strings and DNS answers that no longer fit.

If you flatten, do it only for services with stable, documented ranges, write down what you replaced and when, check the provider’s published ranges regularly, and test after every change. Moving a sender to its own subdomain is usually the cleaner answer.

08Why forwarding breaks SPF

A doctor at a clinic in Muharraq forwards all mail from her clinic address to a personal mailbox at another provider. A hospital sends her a report; the clinic’s server receives it and passes it on. To the final provider, the message now arrives from the clinic’s server, while the envelope still names the hospital’s domain. The clinic’s server is not in the hospital’s SPF record, so SPF fails.

Two things save such messages. DKIM signs the message itself, so the signature usually survives forwarding as long as nobody changes the content; this is the main reason SPF should never stand alone, as DKIM explained shows. And a forwarder can rewrite the envelope sender to its own domain with the Sender Rewriting Scheme, SRS, so SPF is checked against the forwarder. Mailing lists that add footers break DKIM as well, and ARC, covered in our article on MTA-STS, BIMI and ARC, exists for them.

09Worked records for common setups

Each record below goes into your domain’s DNS as one TXT record at the name shown. Replace the documentation addresses with your own, and take each provider’s current include from its own documentation.

Website and email on one hosting server

A clinic in Muharraq keeps its website and mailboxes on one cPanel account, so everything sends from the hosting server:

SPF for a domain whose website and mail share one hosting server
# Name: @ · Type: TXTv=spf1 a mx ip4:203.0.113.25 ~all

Hosting, a mailbox provider and a newsletter service

The shop in Manama keeps its website on hosting and its staff mailboxes on Google Workspace, and sends its newsletter through a mailing service from a subdomain:

The main domain and the newsletter subdomain, each with its own record
# Name: @ · Type: TXTv=spf1 ip4:203.0.113.25 include:_spf.google.com ~all# Name: news · Type: TXTv=spf1 include:servers.mailer.example.com ~all

Notice what is missing: mx, because the MX hosts are Google’s and the include already covers them; and the newsletter service at the main domain, because it has its own subdomain. With Microsoft 365 instead of Google, the include is spf.protection.outlook.com.

Your own mail server on a VPS

A school in Riffa runs its own mail server, with an IPv4 and an IPv6 address:

SPF for a single mail server with IPv4 and IPv6
# Name: @ · Type: TXTv=spf1 ip4:198.51.100.10 ip6:2001:db8:45::10 -all

Listing both addresses matters: if the server sends over IPv6 and only its IPv4 address is listed, SPF fails for every message to providers that receive over IPv6. With one server under your control and DKIM in place, -all is a sound choice.

A domain that never sends mail

A company in Isa Town owns a second domain that it uses only to redirect visitors to its main site. Nothing should ever send as it:

Protecting a domain that sends no mail
# Name: @ · Type: TXTv=spf1 -all# With a DMARC policy of reject# Name: _dmarc · Type: TXTv=DMARC1; p=reject;

This tells every receiver that no server may send as the domain, the cheapest protection against impostors using a domain you are not watching.

10Common SPF problems

Problempermerror: too many DNS lookups
FixCount the lookups including nested includes. Remove unused services, drop a or mx where an include already covers them, and move senders to subdomains.
Problempermerror: more than one record
FixMerge them into one record with one v=spf1 at the start and one all at the end.
ProblemSPF passes but DMARC fails
FixSPF passed for the service’s envelope domain, not yours. Set a custom return path on your domain in the service, or make sure it signs DKIM with your domain.
Problemsoftfail for mail the website sends
FixThe web server sends directly and is not in the record. Add its address, or better, make the site send through your mailbox with authentication.
ProblemOnly forwarded mail fails
FixExpected: forwarding breaks SPF. Make sure DKIM signs your mail, so DMARC can pass on it.
ProblemA typo such as include= or a space after the colon
FixA syntax error makes the whole record permerror. Write include:domain with a colon, no space and no dot at the end.
ProblemThe record changed but receivers still see the old one
FixResolvers keep the old record until its TTL runs out. Wait, then check again.

11SPF at Illevante

On Illevante’s cPanel hosting, SPF and DKIM come with the email on your domain. If you add another sender, such as a newsletter service, you do not add a new SPF record: you add its include to the one you have.

If your domain’s DNS is managed in DNS Console, open the domain’s records, find the TXT record that starts with v=spf1, and edit it rather than adding a second one. Then check the result with the DNS check tool: it confirms that the domain has exactly one SPF record, and if not, shows how to fix it with a button that takes you to DNS Console. It does not check DKIM, so also send a test message to a mailbox you own and read its Authentication-Results header.

If you run your own mail server on CoreVPS, list its IPv4 and IPv6 addresses with ip4 and ip6 as in the school example, and read our guide to a VPS mail server. Next in the series is DKIM, the signature that survives where SPF breaks.

Test your understanding

Three questions, one minute

⁦0 / 3⁩Set 1

  1. 01Which address does SPF check?

  2. 02Your domain has two TXT records that start with v=spf1. What happens?

  3. 03Which of these terms costs no DNS lookup?

Ready to start? Power your projects today

Choose the hosting, server or domain that suits your business, and our specialist team is available around the clock to make your projects a success.

A .com domain a year
$17
cPanel hosting per month
$2
CoreVPS servers per month
$3.50