Mail from your company domain lands in spam or never arrives, and the mail services' reports say “SPF fail” and “DKIM none”. The cause is almost always two DNS records: SPF and DKIM. Here is how they work, how to write them without mistakes and how to check that everything works.
Why SPF and DKIM matter
The mail protocol lets any server send a message on behalf of any domain. The receiving side decides whether to trust a message based on several checks:
- SPF is a list of servers allowed to send mail for the domain. The receiving server looks at the IP the message came from and compares it with the record.
- DKIM is a signature. The sending server signs the headers and body with a private key, and the receiver verifies the signature with the public key from DNS.
- DMARC ties both checks to the address in the From field and says what to do with forgeries. It has its own article: on DMARC.
Large mail providers require SPF and DKIM from everyone who sends a lot of mail, and without them even ordinary correspondence ends up in spam more and more often.
SPF: one TXT record
The record is published on the domain itself as a TXT record:
example.com. TXT "v=spf1 mx include:_spf.google.com ip4:203.0.113.10 ~all"
mxallows the servers from the domain's MX records;ip4:andip6:list specific addresses or networks, such as the web server that sends notifications;include:pulls in a mail service's records:_spf.google.comfor Google Workspace,spf.protection.outlook.comfor Microsoft 365,_spf.yandex.netfor Yandex 360,_spf.mail.rufor VK WorkSpace;- the end says what to do with everyone else:
~all(soft fail, the message is flagged) or-all(hard fail).
Start with ~all: if you forgot a service, its mail is only flagged, not lost. Once DMARC reports show that all legitimate senders are covered, you can move to -all.
Common SPF mistakes
- Two SPF records. A domain must have exactly one TXT record starting with
v=spf1. Two records are an error (permerror), and neither passes. When you add a service, add it to the existing record. - More than 10 DNS lookups. Every
include,a,mx,existsandredirectis a lookup, and nested includes count too. After the tenth, the check breaks.ip4andip6need no lookups, so it is better to replace unnecessary includes with addresses. - The
ptrmechanism is deprecated, slow and ignored by many receivers. - Forgotten senders: the website, CRM, newsletter service, a scanner or multifunction printer, accounting software. Each has its own server, and each must be in SPF or send through your mail service.
- Forwarding. If a message is forwarded through another server, SPF fails at the final recipient, and that is normal. DKIM saves the day: the signature survives forwarding.
DKIM: key and selector
DKIM uses a key pair. The private key stays on the mail server; the public key is published in DNS under a name with a selector, an arbitrary word that the server includes in the signature:
mail._domainkey.example.com. TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."
If your mail is on Google Workspace, Microsoft 365 or Yandex 360, the key is created in their admin console and you only copy the record into DNS. With your own server (Postfix with OpenDKIM or rspamd, Exim), you create the key yourself.
- Key length: 2048 bits. 1024 bits is considered weak.
- Long records. A TXT value longer than 255 characters is split into several quoted strings. Many DNS panels do this for you; in a zone file you split it by hand. If the record “isn't visible”, this is usually the reason, or extra spaces and line breaks picked up while copying.
- Several services mean several selectors. Each sender has its own key and name, and they don't interfere.
- Rotating the key. Publish the new key under a new selector, switch the server to it, and delete the old record a few days later, once old messages have been delivered.
A DKIM key and ready-made DNS lines (SPF with a lookup counter, DMARC, MTA-STS and CAA) are built by the DKIM, SPF, DMARC generator. The key is created in your browser and sent nowhere.
Reverse DNS and the server name
If you run your own mail server, check two more things. The server's IP address needs a reverse record (PTR) with a name that points back to the same address; your hosting provider sets it. And the server must introduce itself with the same name in its greeting (HELO). Without this, mail is rejected at the door. Also check the server's IP against blacklists: an address inherited from a previous owner may have been listed for a long time.
How to check
- The DNS record.
dig +short TXT example.comanddig +short TXT mail._domainkey.example.com, or in the browser with the DNS lookup, which checks SPF, DKIM and DMARC at once. - A real message. Send a message to a mailbox at another provider, open “Show original” and find the
Authentication-Resultsheader. You want to seespf=pass,dkim=passanddmarc=pass. - Header analysis. Paste the full headers into E-mail headers: it shows the check results, the message's path through servers and the delays.
p=none and reports. After a couple of weeks, when the reports show only your own servers, move to p=quarantine and then p=reject. DMARC reports helps you read the reports.