Business tech · Intermediate · 22 min
How to Set Up SPF, DKIM and DMARC So Your Email Is Not Marked as Spam
By Padding · Updated 16 Nov 2026
DNS · any mail provider
You will publish the three DNS records that prove your email is really from your domain, SPF, DKIM and DMARC, check each with dig, stay under SPF's 10-lookup limit, use 2048-bit DKIM keys, and move DMARC safely from p=none to quarantine to reject using its reports.
On this page
Email was designed without proof of origin. Anyone can write any address in the "From" line, which is how phishing emails pretend to be your bank, or your company. Mailbox providers fight back by checking three DNS-based signals before they trust a message from your domain. If those signals are missing, your genuine email lands in spam, and anyone can send forged email in your name.
- SPF (Sender Policy Framework) is a list, published in DNS, of the servers that may send mail for your domain.
- DKIM (DomainKeys Identified Mail) adds a cryptographic signature to every message. The receiver fetches your public key from DNS and checks the signature.
- DMARC (Domain-based Message Authentication, Reporting and Conformance) ties them together: it tells receivers what to do with mail that fails, and where to send you reports about it.
You set each up as a TXT record in DNS, so you need access to your domain's DNS. Large mailbox providers now expect this. Since 1 February 2024, Gmail requires SPF or DKIM from everyone who sends to Gmail addresses, and SPF, DKIM and DMARC from anyone who sends 5,000 or more messages a day to Gmail accounts (https://support.google.com/a/answer/81126). Even for a small sender, these records are the difference between the inbox and the spam folder.
Your mail provider's own instructions give the exact values to publish. This tutorial explains what each record means, so you can enter them correctly, avoid the common mistakes, and check the result.
Step 1 · List everything that sends email as your domain
Before you write any record, find every system that sends mail with your domain in the From address. A missed one will fail the checks, and later DMARC enforcement will reject its mail. Common ones:
- your main mailbox service (Google Workspace, Microsoft 365, Fastmail and so on)
- a service that sends transactional email for your website, such as receipts and password resets
- a newsletter or marketing tool
- your own server, if it sends mail (for example from a contact form or a cron job)
- helpdesk, invoicing and CRM tools that send "on your behalf"
Write the list down. For each, find its "email authentication" or "domain setup" page, which gives the SPF include and the DKIM records you need.
Step 2 · Publish an SPF record
An SPF record is a TXT record at the root of your domain (@) that starts with v=spf1. It lists who may send, then says what to do with everyone else. A typical record for one provider looks like this:
v=spf1 include:_spf.google.com ~allRead it left to right. v=spf1 marks it as SPF. include:_spf.google.com says: also accept whatever servers that name authorises. Google's own domain uses the same pattern; ask for its record with dig +short TXT google.com and you find a line starting "v=spf1 include:_spf.google.com ~all" among the text records. The last term, ~all, covers everything not listed, and it has three flavours:
~all(soft fail): mail from other servers should be treated with suspicion. This is the usual setting while you set things up and, with DMARC in place, the one most domains keep.-all(hard fail): mail from other servers should be rejected. Use it when you are sure the list is complete.?allor+all: do nothing, or allow everyone. Never use+all: it makes the record meaningless.
To allow several senders, put them all in one record:
v=spf1 include:_spf.google.com include:sendgrid.net ip4:203.0.113.10 ~allHere the second include is a mail-sending service, and ip4:203.0.113.10 is a server of your own (use your real address). Replace each include with the value your provider documents.
Three rules matter more than any other:
- One SPF record per name. If you publish two
TXTrecords that both start withv=spf1, receivers treat the result as a permanent error, and SPF fails for all your mail. When you add a sender, edit the existing record, do not add a second one. - At most 10 DNS lookups. While checking a message, the receiver follows
include,a,mx,ptr,existsandredirectterms, and each one that requires a DNS lookup counts. Past 10 in total, nested includes included, the check fails with a permanent error (permerror). The termsip4,ip6andallcost nothing. A provider's include can itself contain further includes, so a record with only five includes can still be over the limit. If you are close, remove senders you no longer use, and useip4:ranges for servers of your own instead ofaandmx. The limit is set in RFC 7208, section 4.6.4. - Text strings are limited to 255 characters. A long record is split into several quoted pieces in one
TXTrecord. Most DNS dashboards do this for you; a few ask you to split it yourself.
Add the record in your DNS dashboard as type TXT, name @ (or blank), and the whole value without the surrounding quotation marks unless the dashboard asks for them.
Check it worked
dig +short TXT example.com | grep spf1Replace example.com with your domain. You should see exactly one line with v=spf1. A mail provider's check page, or an online SPF checker, counts the lookups for you and tells you if you are over 10.
Step 3 · Turn on DKIM signing
DKIM works with a pair of keys. Your mail provider keeps the private key and signs every outgoing message with it, adding a DKIM-Signature header. You publish the public half in DNS so receivers can verify the signature. You do not create the keys by hand, unless you run your own mail server: the provider's setup page generates them and shows you one or more DNS records to add.
The record is a TXT (or sometimes a CNAME pointing to the provider's key) at a name of the form selector._domainkey.example.com. The selector is a label the provider chooses, such as google, s1 or selector1, so that a domain can have several keys at once. The value looks like this:
v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...p= is the public key in base64. These are two points to check:
- Key length. Use a 2048-bit RSA key. The standard says signers must use at least 1024 bits and should use at least 2048 (RFC 8301). A 2048-bit key makes a
p=value over 255 characters, which has to be split into several quoted strings inside oneTXTrecord. Many dashboards do the splitting for you; if yours does not, you split the value yourself at any point and put each part in its own quotation marks. If the provider offers only a 1024-bit key, ask about 2048. - Paste exactly. An extra space, a missing character or a line break in the key makes every signature fail. Copy the value with the provider's copy button, not by hand.
After you add the record, go back to your provider and press its "verify" or "start signing" button. Many providers do not sign until you do.
Check it worked
Ask DNS for the key. Replace the selector and the domain with the ones your provider gave you:
dig +short TXT selector1._domainkey.example.comYou get back the v=DKIM1; k=rsa; p=... text. If you get nothing, the name is wrong. A common mistake is that the dashboard added your domain a second time. Then send a test email to a Gmail address, open it, choose "Show original" from the menu and look for DKIM: 'PASS' with domain example.com. In any mail program, view the full headers and look for dkim=pass in the Authentication-Results header.
Step 4 · Start DMARC in monitoring mode
SPF and DKIM prove that a message passed some check, but not that the check matches the visible From address. DMARC adds that. It passes a message when SPF or DKIM passes and the domain it checked lines up ("aligns") with the domain in the From line, and it tells receivers what you want done with failures.
The record is a TXT at the name _dmarc.example.com. Start with this one:
v=DMARC1; p=none; rua=mailto:dmarc-reports@example.comv=DMARC1marks it.p=nonemeans: do not treat failing mail differently, only report. This is monitoring mode, and it is where every rollout starts.rua=is where receivers send aggregate reports, XML summaries of who is sending mail as your domain and whether it passes. Replace the address with a real mailbox, preferably a dedicated one, because the reports are frequent and machine-readable. Many free and paid services read them and turn them into a dashboard; pointruaat one of them if you do not want to read XML.
If the rua address is in a different domain from the one you are protecting, the receiving domain must agree, with a TXT record at example.com._report._dmarc.reports-domain.net containing v=DMARC1;, where example.com is your domain (RFC 9990). Report services tell you the exact record to add. Without it, receivers skip your reports.
Google's own record is a real-life example, at _dmarc.google.com:
v=DMARC1; p=reject; rua=mailto:mailauth-reports@google.comThat is a mature domain at the end of the rollout: policy reject, and reports still collected.
Check it worked
dig +short TXT _dmarc.example.comYou should see your record on one line. After a day or two, reports start arriving in the rua mailbox from the big providers.
Step 5 · Read the reports and fix what fails
Leave p=none on for at least a month, longer if you send mail rarely, so that the reports cover a full cycle (monthly newsletters, quarterly invoices, the system that only runs at year end). The DMARC specification itself recommends beginning at p=none with reporting and treating the reports as an audit of your own mail systems, because it is common to forget a sender (RFC 9989, sections 5.1.4 and 5.1.5). Each report lists the sending IP addresses, how many messages they sent, and whether SPF and DKIM passed and aligned.
What you do with each line:
- A legitimate sender that fails: you forgot to add it to SPF, or its DKIM is not set up. Fix that sender's records with its provider's instructions.
- A sender that passes SPF or DKIM but fails the DMARC domain match: the message passed a check, but for a different domain. A mail provider that sends with its own return domain, for example, passes SPF for itself, not for you. Switch on "custom domain" or "branded sending" in that provider so DKIM is signed with your domain.
- Senders you do not recognise: these are either forgotten services or forgery attempts. The second kind is what the policy will stop.
Do not move on until every legitimate source passes.
Step 6 · Tighten the policy in stages
When the reports are clean, raise the policy from none in steps. Each change is the same record with a different p=:
| Stage | Record value | What receivers do with failing mail |
|---|---|---|
| 1. Monitor | p=none | Deliver as usual; send reports |
| 2. Quarantine | p=quarantine | Treat as suspicious, usually the spam folder |
| 3. Reject | p=reject | Refuse it |
Stay at each stage for weeks and keep watching the reports for legitimate mail that now fails. Mail that fails at quarantine ends up in spam, which is recoverable; at reject it bounces. The specification advises at least a month at none, then an equal time at quarantine, before reject (RFC 9989, section 7.4).
Two cautions. Mailing lists and forwarding services often change messages in ways that break SPF and DKIM, so a strict policy can cause list mail from your users to be rejected. RFC 9989 says that domains which publish p=reject should tell their users not to post to mailing lists, or warn them that it may not work (section 7.4), and that domains used for general-purpose email, where people write from ordinary mailboxes, should not use p=reject at all (appendix C.6). If your domain hosts people's everyday mailboxes, stay at quarantine. And older guides mention a pct= tag to apply a policy to only a percentage of mail. The current DMARC specification, RFC 9989 from May 2026, removed it. It marks pct as historic and adds a t=y testing flag for receivers instead, so use stages of time, not percentages, and see what your receivers say in their reports.
Once your main domain is protected, also cover names that do not send mail, so nobody can forge them. For subdomains of the main domain, the policy is inherited unless you set sp= (a different policy for subdomains) or np= (for names that do not exist). A domain that sends no mail at all can publish v=spf1 -all and v=DMARC1; p=reject.
Troubleshooting
- DMARC passes in your tests but a real mail stream fails: it comes from a service you did not list. Check the report for its IP address.
- SPF
permerrorin a mail header: you have two SPF records, or you are over 10 lookups. dkim=fail (bad signature): the key in DNS does not match the key the provider signs with, usually from a copy and paste error or a key that was regenerated. Copy the record again.dkim=none: the provider is not signing. Press its verify button, or check that you added the record at the right name.- You changed a record and
digshows the old one: DNS answers are cached for the record's TTL. Ask the authoritative nameserver, as in point a domain at your website. - Your forwarded mail fails: forwarding breaks SPF by design. DKIM survives forwarding, which is why you want it.
Next steps
- Understand the record types in more depth: DNS records explained.
- Connect the domain to your website: point a domain name at your website.
- Send transactional email from your app with the same domain, and watch the DMARC reports for a month before tightening the policy.
Related tutorials
- DNS Records Explained: A, AAAA, CNAME, MX and TXT, with dig ExamplesYou will learn what the common DNS record types do and read each one with dig, using a small practice DNS server you start yourself.
- How to Point a Domain Name at Your WebsiteYou will connect a domain you own to a website on any host by adding the right A, AAAA and CNAME records at your DNS provider, test the site before DNS changes, and check with dig that the change has reached the world.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials