How to Set Up an SPF Record for Your Domain

An SPF record is one DNS TXT record at your domain's owner name. Here is what goes in it, what breaks it, and how it works with DKIM and DMARC.

An SPF record for a domain is a DNS TXT record. It lists the mail servers allowed to send as that domain, so a receiver can spot mail that pretends to. Here is how to publish one that works, and the mistakes that quietly break it.

What an SPF record is

The record is one string of text in the RDATA of a single DNS TXT resource record. It lives at the owner name it belongs to, which for most senders is the bare domain. It does not go in a subdomain under that owner name unless you mean to authenticate that subdomain on its own.

SPF records must be published as a DNS TXT resource record only. Other DNS record types were supported in SPF's experimental phase, but that has been discontinued. If a checklist tells you to publish a type SPF record, that checklist is out of date.

How the record string is formatted

Every record starts with a version tag, then a list of terms, mostly mechanisms. SPF's standard says an SPF directive is an optional qualifier followed by a mechanism. The qualifier says what happens when the term matches.

  • v=spf1 comes first. Without it, the record is not an SPF record at all.
  • ip4: and ip6: name sending addresses or ranges directly.
  • a and mx point at the domain's own A or MX records.
  • include: hands the check to another domain's SPF record, which is how you add a sending service.
  • ~all or -all end the record and set the result for mail from anywhere else.

The whole thing is encoded as US-ASCII. Keep it on one line in your DNS panel.

The rules that make a record valid

Three rules do most of the work.

First, a domain must not have multiple records that would make an authorization check select more than one record. One domain, one SPF record. If two exist, SPF's standard says check_host returns permerror, and the error is not about your IP list. It is about the extra record.

Second, if a published record holds multiple character strings, the record must be treated as if those strings are joined together with no spaces added. Some DNS panels split long values into strings. The join is legal, but a space added at the split point breaks the record. Check the raw output, not just the panel's input box.

Third, the record must be TXT. A domain often has other TXT records for verification codes and the like. Those are fine. Only one of them may start with v=spf1.

Common publishing mistakes

The mistakes above cause most SPF failures. A few more show up often.

Placing the record on a subdomain when mail goes out as the bare domain. The check looks at the domain in the envelope, and it will not find your record.

Using a wildcard to publish SPF. Use of wildcard records for publishing is discouraged, and care has to be taken if they are used. A wildcard can match names you did not intend and break the check for them.

Bloat from other TXT records. Since TXT records have many uses, other TXT records published there for other purposes might cause problems with size limits. If your SPF record plus its neighbours pushes past what DNS responses carry cleanly, trim the list.

Too many include: hops. Each one adds DNS lookups. The standard says publishers ought to keep the DNS information needed to evaluate a record to a minimum. If you stack services, count the lookups and drop the ones you no longer use.

How SPF fits with DKIM and DMARC

SPF alone does not decide where your mail lands. It answers one question: did this message come from a server the domain allows? DMARC's standard says SPF and DKIM provide domain-level authentication but are not tied to the Author Domain. That is the domain in the From: header the reader sees.

DMARC closes that gap. The standard says a message needs alignment between the Author Domain and an Authenticated Identifier to achieve a DMARC pass. With SPF, the identifier is the MAIL FROM domain. The standard calls an SPF-validated MAIL FROM identity the "SPF Domain".

Alignment comes in two strengths. Relaxed alignment means the Author Domain and the Authenticated Identifier share the same Organizational Domain, so mail from a subdomain can pass. Strict alignment means the Author Domain is identical to the Authenticated Identifier.

A receiver that takes part performs SPF checks, DKIM checks, and a DMARC Policy Record lookup before replying to the DATA command. Your DMARC policy then tells it what to do with mail that fails. Gmail's guidelines put it simply: DMARC tells receiving servers what to do with messages that don't pass SPF or DKIM.

This is why a pass on one path is enough. Gmail's FAQ says bulk senders must set up both SPF and DKIM, but only one needs to be aligned. If your SPF is broken but DKIM is aligned, DMARC can still pass.

What Gmail and Yahoo expect

Gmail's guidelines say all senders must set up SPF or DKIM email authentication for their sending domains. Bulk senders get the fuller list: Gmail's guidelines say bulk senders must set up SPF, DKIM, and DMARC for their domain.

Gmail's guidelines say the SPF record for your domain should include all email senders for your domain. If third-party senders are not in your SPF record, their messages are more likely to be marked as spam. Gmail's FAQ says messages must be authenticated with both SPF and DKIM.

When SPF fails, Gmail says so in the bounce. Gmail's FAQ says error code 4.7.27 means SPF authentication didn't pass. Gmail's FAQ says error code 5.7.27 means the message was blocked because it didn't pass SPF authentication. The same guidelines warn that mail without authentication might be rejected with a 5.7.26 error or sent to spam.

Yahoo's FAQ says SPF records let Yahoo reject messages that come from IPs not listed in the domain's SPF record. So an incomplete record has real consequences there, not just a spam folder.

If you use an email service provider, Gmail's guidelines say to verify that they authenticate your domain's email with SPF and DKIM. Most providers publish an include: term for exactly this. Ask them for it rather than guessing.

Checking and keeping the record right

Look up your domain's TXT records and read the raw strings. Confirm exactly one record starts with v=spf1. Check that no space crept in at a string join, and that every service you send through is listed.

Then re-check when anything changes. A new sending tool, or your own sending server moving to a new address, can leave an old record wrong. Gmail's guidelines say authenticated messages are less likely to be rejected or marked as spam, so the record earns its keep every day you send.

SPF is one piece of the setup. Pair it with DKIM and a DMARC policy, and add the one-click unsubscribe headers Gmail asks for. Unlike a dead blocklist like SORBS, SPF is a live rule, and it is yours to get right.

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
  • 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
  • SPF record syntax, tag by tag
  • What an SPF Record Is and How to Read Yours