SPF record syntax, tag by tag

SPF is one of the two checks DMARC builds on. Here is how to read and write an SPF record without breaking the chain.

SPF record syntax is short to read and easy to break. The record is one line of text in DNS, but each part has rules. A mistake in that line does not just fail SPF. It can also cost the message its SPF path to a DMARC pass, because DMARC builds on SPF as one of its two underlying checks. Gmail's guidelines say to pass DMARC authentication, messages must be authenticated by SPF or DKIM, or both. So a broken SPF record removes one of the two paths a message has to pass. This guide walks the syntax from left to right, then covers the mistakes that break it and how to check your work before you publish.

Where an SPF record lives

An SPF record is a DNS TXT record placed at the owner name it belongs to, not in a subdomain under that owner name. For a domain like example.com, publish the record at example.com itself. The record for example.com does not go in a subdomain under it. Receivers look it up based on the domain in the envelope, which is the address used in the SMTP MAIL FROM command.

DNS once had a record type made just for SPF. It is dead. Publish the record as a TXT record. Some DNS consoles still offer the SPF type, and some tools still accept it, but receivers query TXT. If you publish in the old type only, receivers will not see your policy. SPF's standard says the DNS query for an SPF record is made for type TXT only.

The anatomy of an SPF record

Every SPF record starts with a version tag:

v=spf1...

The v=spf1 tag must come first. If it does not, the receiver treats the whole record as not an SPF record at all and ignores it. There is no second version to use. Everything after v=spf1 is an ordered list of terms, mechanisms and modifiers, separated by spaces. SPF's standard says it is better to end with either a redirect modifier or an all mechanism.

Here is a complete example:

v=spf1 ip4:198.51.100.5 include:_spf.examplevendor.com -all

Read it as a list of tests. The receiver walks them left to right and stops at the first one that matches the connecting server.

Mechanisms, one by one

Each mechanism names a way to match a sending server.

  • include:domain tells the receiver to fetch the SPF record at that domain and evaluate it. Use it to pull in another party's senders, like your email vendor.
  • a matches if the sending domain's A or AAAA record holds the connecting IP.
  • mx matches if the domain's MX hosts resolve to the connecting IP.
  • ip4:address and ip6:address match one exact address or a whole block written in CIDR notation, such as ip4:198.51.100.0/24. These cost the receiver no extra DNS lookups, which makes them cheap to evaluate.
  • exists:domain constructs a domain name for a DNS A record query and matches if any A record is returned.
  • ptr asks the receiver to do a reverse DNS lookup on the connecting IP. It is slow, unreliable, and discouraged. Do not use it.
  • all matches everything left over. It is the catch-all at the end of the record.

Order matters. The receiver stops at the first match, so a broad mechanism placed early will swallow a narrow one placed later. Put specific matches first and all last.

Qualifiers: what a match means

Each mechanism can carry a qualifier prefix that decides what a match means.

  • + means pass. It is what the record assumes when you leave the sign off, so +ip4:198.51.100.5 and ip4:198.51.100.5 mean the same thing.
  • - means fail. A match returns fail, and SPF's standard says the action to take is determined by the local policies of the receiver.
  • ~ means softfail. A match is a weak statement that the host is probably not authorized.
  • ? means neutral. A match says nothing either way.

So the difference between -all, ~all, ?all and +all is the policy for every server you did not list. -all says they fail. ~all gives them softfail, and SPF's standard says receiving software should not reject a message based solely on a softfail result. ?all says no opinion. +all says every server on earth passes, which defeats the point of the record. Never publish it.

Common syntax mistakes

These are the errors that make a record fail or get ignored.

Two SPF records on one domain. A domain can have only one. If a receiver finds two, SPF's standard says check_host returns permerror. This happens when a vendor asks you to add their include and you publish a second record instead of editing the first. Merge everything into one line.

Missing or misplaced v=spf1. The version tag must be the first thing in the record. SPF's standard says records that do not begin with a version section of exactly v=spf1 are discarded.

Wrong ordering. A mechanism after all is never evaluated, because all matches everything. Keep all at the end.

The 10-lookup limit. SPF's standard says the include, a, mx, ptr, and exists mechanisms and the redirect modifier cause DNS queries. It limits the total number of terms that cause DNS queries to 10. Too many includes, especially nested ones, push you over. When that happens, SPF's standard says the implementation must return permerror. Count your lookups before you publish, not after.

Character-string limits. Long records are often published as several character strings. Some DNS consoles split a long record into multiple strings for you. That is fine. SPF's standard says a published record with multiple character-strings must be treated as if those strings are concatenated together without adding spaces. What is not fine is a manual split in the wrong place, or a console that truncates instead of splitting. Keep the record short where you can.

Stray characters. A smart quote or a missing space between mechanisms can make the record unparseable. SPF's standard says check_host returns permerror right away if the SPF record has any syntax errors. Type it as plain text.

How SPF connects to DMARC

SPF on its own authenticates the envelope domain, not the From header a reader sees. DMARC's standard says SPF and DKIM provide domain-level authentication but are not directly associated with the Author Domain. DMARC is what ties them together. The standard says a message only passes DMARC when an Authenticated Identifier aligns with the Author Domain.

For SPF, the Authenticated Identifier is the envelope domain. DMARC's standard says DMARC relies solely on SPF validation of the MAIL FROM identity. The term "SPF Domain" refers to an SPF-validated MAIL FROM identity. Alignment then asks whether that domain matches the Author Domain. DMARC's standard defines the Author Domain as the domain name of the apparent author as extracted from the RFC5322.From header field.

There are two modes. Google Workspace Help says relaxed SPF alignment is the default and accepts any valid subdomain of the domain name. Strict alignment requires an exact match between the Envelope-Sender address domain and the From header address domain. Google Workspace Help says a message passes DMARC if it passes SPF authentication with SPF alignment or DKIM authentication with DKIM alignment. It fails if it fails both.

This is where syntax errors bite. If your SPF record is broken, the SPF check returns permerror rather than pass, so there is no SPF-Authenticated Identifier to align. Your DMARC policy then leans entirely on DKIM. If DKIM is also missing, the message fails DMARC outright. Google Workspace Help says to set up DKIM and SPF before setting up DMARC. Yahoo's guide strongly urges all senders to publish a DMARC policy for each domain that sends mail. Fix the SPF line first.

How to check your record before and after publishing

Before you publish, count your DNS lookups by hand. Add up every include, a, mx, exists and ptr mechanism and any redirect modifier. If you are near 10, cut mechanisms or replace includes with ip4 entries.

After publishing, query the record directly. From a terminal, run dig TXT yourdomain.com, or paste the domain into a public SPF checker. Read the raw text first, then read the tool's evaluation. Check that the version tag is first and that there is exactly one record. Then check that the evaluation ends in the result you expect for a known sending IP.

After that, mail yourself a test and open the headers to read them. Look for the SPF result line, which should say pass and name your domain. SPF's standard says SPF results can be recorded in the Received-SPF header field and in the Authentication-Results header field. That line tells you exactly what the receiver saw.

What SPF does not do

SPF authenticates the envelope domain. It says nothing about the From header on its own. SPF can return a pass while DMARC still fails, when the envelope domain and the From domain do not match up. SPF also says nothing about the message body, so it cannot detect a spoofed display name. And SPF breaks on forwarding, because the forwarder's server is not in your record.

That is why DMARC exists, and why Gmail's guidelines say DMARC tells receiving servers what to do with messages that don't pass SPF or DKIM. SPF is one leg of the table. Write the record cleanly, publish it once, and check it after. Then handle the other rules a sender faces, like the one-click unsubscribe headers Gmail asks for. A dead list like SORBS cannot hurt you, but a broken SPF record can.

Sources

Filed

More filed under SPF and DKIM

  • How to find all DNS records for a domain
  • How to Set Up a DKIM Record for Your Domain
  • How to Set Up a DKIM Email Record
  • What a DKIM record is and what each part does
  • Why Your Email Failed SPF and What to Do
  • Can a Domain Have Multiple SPF Records?
  • What It Means When a Domain Has No SPF Record
  • How to Set Up an SPF Record for Your Domain
  • What SPF Email Authentication Checks and Why It Matters
  • How to Set Up a Gmail SPF Record
  • What an SPF Record Is and Where It Goes in DNS
  • What an SPF Record Is and How to Read Yours