Running a server
How to set up SPF, DKIM and DMARC
A step-by-step guide to SPF, DKIM and DMARC for your own mail server: what each record does, how to publish it, how to test it and common mistakes.
On this page
Large mailbox providers no longer accept mail from servers that cannot prove who they are. Three DNS records do that: SPF lists the servers allowed to send for your domain, DKIM signs every message with a key published in DNS, and DMARC ties both to the address people see in the From line and tells receivers what to do when the check fails.
This guide applies to any self-hosted mail server; the RustPost specifics are marked. The examples use the domain example.org, the mail host mail.example.org and the address 203.0.113.25. For the exact values of your own server, the RustPost admin console lists every record and checks it live: see DNS records.
Why all three
- SPF alone checks the envelope sender (the
Return-Path), which people never see, so a forger can pass SPF with their own domain and still put yours in the From line. SPF also breaks when a message is forwarded. - DKIM alone proves that a domain signed the message, but not that it is the domain in the From line.
- DMARC passes only when SPF or DKIM passes for the domain in the From line (this is called alignment), and publishes your policy for mail that does not.
Gmail and Yahoo require SPF or DKIM from everyone who sends to them, and all three from senders of more than 5,000 messages a day; Outlook.com applies the same rule to large senders. Even a small server gets less mail sent to spam with all three in place.
Before you start
The receiving side also looks at the sending host itself, so check these first:
- an A (and AAAA, if you have IPv6) record for
mail.example.org; - a PTR (reverse DNS) record for each address, naming
mail.example.org. Your hosting provider sets it, not your DNS zone; - the server greets with the same name (
EHLO mail.example.org); - an MX record for the domain.
The DNS page shows these records for RustPost.
Step 1: SPF
SPF is one TXT record at the domain itself:
example.org. TXT "v=spf1 mx -all"Read it left to right: mx allows the hosts in the domain's MX record, and -all says every other host fails. Other common mechanisms:
| Mechanism | Allows |
|---|---|
mx | the hosts in the domain's MX record |
a | the domain's own A and AAAA addresses |
ip4:203.0.113.25, ip6:2001:db8::25 | one address or a network (ip4:203.0.113.0/24) |
include:_spf.relay.example | everything another domain's SPF record allows, for a relay or a newsletter service |
Rules worth knowing:
- One SPF record per name. Two
v=spf1records make SPF fail with a permanent error. Merge them into one. - At most 10 DNS lookups.
include,a,mx,existsandredirecteach cost one, and so do the includes inside an include. Past ten, SPF fails. Preferip4:andip6:for your own servers. - End with
-allor~all.-all(fail) is the clear choice once the list is complete;~all(soft fail) is for while you are still finding every sender. Never publish+all, which allows the whole internet. - Add every service that sends as your domain: a smarthost, a ticketing system, an invoicing tool.
- Domains and subdomains that send no mail should say so:
"v=spf1 -all".
Step 2: DKIM
The server signs each outgoing message with a private key. Receivers fetch the public key from DNS at <selector>._domainkey.<domain> and check the signature:
rsa-20261129._domainkey.example.org. TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqh…"- The selector names the key, so several keys can be published at once, which is what makes rotation possible.
- Use RSA with 2048 bits. 1024-bit keys are considered weak, and some DNS providers have trouble with 4096-bit keys. An Ed25519 key is shorter and faster, but not every receiver checks it yet, so publish it next to an RSA key, never instead of one.
- Long values: a TXT string holds at most 255 characters, so a 2048-bit key must be split into several quoted strings in the same record (
"v=DKIM1; k=rsa; p=MIIB…" "…IDAQAB"). Many DNS providers do this for you. Do not add spaces or line breaks inside the key. - Rotate keys now and then: publish the new key, wait until DNS serves it, start signing with it, and remove the old record a few days later, once mail signed with it has been delivered.
With RustPost, a domain's first keys are created automatically, the selectors are named after the creation date (rsa-YYYYMMDD, ed-YYYYMMDD), and the console shows each record to copy. On the command line:
docker compose exec rustpost rustpost domain dkim example.orgRotation is described in the admin guide, and Ed25519 signing is the dkim_ed25519 setting in [outbound].
Step 3: DMARC
DMARC is a TXT record at _dmarc.<domain>:
_dmarc.example.org. TXT "v=DMARC1; p=none; rua=mailto:dmarc-reports@example.org"| Tag | Meaning |
|---|---|
p | The policy for mail that fails: none (only report), quarantine (to spam) or reject (refuse). |
rua | Where receivers send daily aggregate reports: which servers sent mail as your domain and whether it passed. |
sp | The policy for subdomains, if it should differ from p. |
adkim, aspf | Alignment: r (relaxed, the default) accepts a subdomain such as mail.example.org for example.org; s (strict) needs the exact domain. |
A message passes DMARC when at least one of these is true:
- SPF passes and the envelope sender's domain matches the From domain;
- a DKIM signature verifies and its
d=domain matches the From domain.
That is why DKIM matters most: it survives forwarding, while SPF does not.
If the reports go to an address at another domain, that domain must agree to receive them with a record such as example.org._report._dmarc.reports.example.net TXT "v=DMARC1".
Roll out DMARC in stages
Going straight to p=reject can make your own mail disappear if one sender was forgotten. The safe path:
p=nonewithrua. Nothing changes for delivery; reports start to arrive the next day.- Read the reports for two to four weeks. They are XML files; a DMARC report viewer or an online analyzer makes them readable. Every legitimate source should pass SPF or DKIM with alignment. Fix the ones that do not.
p=quarantine. Failing mail goes to spam. Keep reading the reports.p=reject. Failing mail is refused. This is what protects your domain from being forged.
The RustPost console warns while a domain's policy is p=none or has no rua.
Test the records
Look up what DNS actually serves:
dig +short TXT example.org
dig +short TXT rsa-20261129._domainkey.example.org
dig +short TXT _dmarc.example.orgThen send a message to an address at a large provider and open its headers. In Gmail, Show original sums it up as SPF: PASS, DKIM: PASS and DMARC: PASS. Elsewhere, look for the Authentication-Results header:
Authentication-Results: mx.google.com;
dkim=pass header.i=@example.org header.s=rsa-20261129;
spf=pass smtp.mailfrom=alice@example.org;
dmarc=pass (p=NONE) header.from=example.orgOnline deliverability testers that give you an address to write to do the same check, and they also look at reverse DNS and blocklists.
Common mistakes
- Two SPF records, often one left over from a previous provider: SPF then fails everywhere.
- More than 10 lookups after adding a few
include:s. - A sender left out: the contact form or the invoicing tool that sends as your domain fails DMARC once the policy is
quarantineorreject. - A broken DKIM key: a missing piece when the value was split, or extra quotes and spaces pasted into it.
- DMARC that never moves past
p=none: it reports, but protects nothing. - Mailing lists and forwarding: they break SPF, and lists that change the subject or footer break DKIM too. Senders cannot fix this; receivers handle it with ARC and by trusting the list.
How RustPost uses these records
- Outgoing mail is signed with each domain's DKIM keys (RSA, plus Ed25519 when enabled), and the DNS check explains in plain words what is wrong with your SPF, DKIM or DMARC records.
- Incoming mail is checked against the sender's SPF, DKIM and DMARC. When DMARC fails and the sender's domain publishes
p=rejectthe message is refused, withp=quarantineit goes to Junk, and each failure adds to the spam score. See[antispam].