Log inSign up

Make your mail trusted with SPF, DKIM and DMARC

Publish an SPF TXT record listing the hosts allowed to send for example.net, sign outgoing mail with a DKIM key (OpenDKIM as a Postfix milter) and publish the public key under selector._domainkey, then add a DMARC record at _dmarc.example.net starting with p=none. Before any of this matters, ask support by ticket whether outgoing port 25 is open and for a PTR record, and plan to send through an authenticated relay on port 587, which must then appear in your SPF record.

Topic: Email servers · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Settle the sending route before writing records

Your records must describe the path mail really takes, so decide that path first. Open a support ticket and ask whether outbound port 25 is available for your server; it is not promised. If it is closed, or you want delivery that does not depend on the reputation of a new address, send through an authenticated SMTP relay on port 587 as shown in send app mail through an SMTP relay. In the same ticket, ask for reverse DNS (PTR) on 198.51.100.24 and 2001:db8:4f2a::24 pointing to mail.example.net. Receivers compare PTR, the HELO name and the A record, and a mismatch costs trust even when SPF and DKIM pass.

What does each record actually prove?

  • SPF lists the IP addresses and services allowed to send with your domain in the envelope sender. It is a TXT record on the domain itself.
  • DKIM adds a cryptographic signature to each message. Receivers fetch your public key from DNS and confirm the headers and body were not altered.
  • DMARC ties the two to the visible From address and tells receivers what to do when neither aligns: nothing, quarantine or reject. It also requests reports.

A message passes DMARC when at least one of SPF or DKIM passes and matches the From domain. DKIM survives forwarding, SPF usually does not, so signing every message is the most important of the three.

How do I write the SPF record?

Create one TXT record on example.net. For a server that sends directly:

example.net.  TXT  "v=spf1 ip4:198.51.100.24 ip6:2001:db8:4f2a::24 -all"

When outgoing mail goes through a relay, add the include value your relay documents, for example:

example.net.  TXT  "v=spf1 ip4:198.51.100.24 include:spf.relay.example -all"

Rules worth remembering: publish exactly one SPF record per name, keep the total of DNS lookups (each include, a, mx) at ten or fewer, and use ~all (soft fail) while you are still discovering senders, switching to -all once you are sure. Domains that never send mail should publish v=spf1 -all.

Install OpenDKIM and create a key

OpenDKIM signs mail that Postfix hands it through the milter interface.

sudo apt install opendkim opendkim-tools                    # Ubuntu, Debian
sudo dnf install epel-release && sudo dnf install opendkim opendkim-tools   # Rocky, Alma
opendkim -V | head -n 1
sudo mkdir -p /etc/opendkim/keys/example.net
sudo opendkim-genkey -b 2048 -d example.net -s s2026 -D /etc/opendkim/keys/example.net
sudo chown -R opendkim:opendkim /etc/opendkim/keys
sudo chmod 600 /etc/opendkim/keys/example.net/s2026.private

The selector s2026 is just a label; dating it makes key rotation obvious later. The command writes s2026.private (keep it secret) and s2026.txt (the DNS record).

How do I connect OpenDKIM to Postfix?

Edit /etc/opendkim.conf so these lines are present and any other Socket, Mode, Domain, Selector or KeyFile lines are commented out:

Mode             sv
Domain           example.net
Selector         s2026
KeyFile          /etc/opendkim/keys/example.net/s2026.private
Canonicalization relaxed/simple
Socket           inet:8891@localhost

Then tell Postfix to pass mail through the milter and restart both:

sudo postconf -e "smtpd_milters = inet:localhost:8891"
sudo postconf -e "non_smtpd_milters = inet:localhost:8891"
sudo postconf -e "milter_default_action = accept"
sudo systemctl enable --now opendkim
sudo systemctl restart opendkim postfix
sudo ss -tlnp | grep 8891

If nothing listens on 8891 on Ubuntu or Debian, check /etc/default/opendkim for a SOCKET line that overrides the file and set it to inet:8891@localhost as well. On Rocky and Alma with SELinux enforcing, port 8891 is already labelled for milters.

Publish the DKIM public key

Print the generated record:

sudo cat /etc/opendkim/keys/example.net/s2026.txt

Create a TXT record named s2026._domainkey.example.net whose value starts with v=DKIM1; k=rsa; p= followed by the long key. The file splits the key into several quoted pieces; most DNS panels want the pieces joined into a single string without the quotes, and they split it into 255 character chunks themselves. Test once DNS has updated:

sudo opendkim-testkey -d example.net -s s2026 -vvv

The line key OK confirms the published key matches the private file. A note about key not secure only means DNSSEC is not in use and is harmless. If you send through a relay that signs with its own key, publish the record it gives you as well so its signature aligns with your domain.

How should the DMARC record start out?

Begin in monitoring mode so you learn who sends for your domain before anything is blocked:

_dmarc.example.net.  TXT  "v=DMARC1; p=none; rua=mailto:dmarc@example.net; fo=1"

Aggregate reports arrive daily as compressed XML at the rua address. After two to four weeks with every legitimate source passing, move to p=quarantine, optionally with pct=25 to apply it to a quarter of failing mail first, and later to p=reject. A strict policy is also what large mailbox providers look for from domains that send in volume.

Verify everything from the command line

Query each record and the reverse entry with dig (package dnsutils on Debian and Ubuntu, bind-utils on Rocky and Alma):

dig +short TXT example.net
dig +short TXT s2026._domainkey.example.net
dig +short TXT _dmarc.example.net
dig +short MX example.net
dig +short -x 198.51.100.24

The last line must return mail.example.net.; if it shows a generic name, the PTR ticket is still pending. Then send a message to an outside mailbox, open its original source and find the Authentication-Results header. You want spf=pass, dkim=pass with header.d=example.net and dmarc=pass. More lookup techniques are in DNS lookups with dig.

Rotate keys and keep records tidy

Replace the DKIM key about once a year: generate a key with a new selector such as s2027, publish its record, switch Selector and KeyFile, restart OpenDKIM and remove the old record a week later. Whenever you add a sender (a newsletter tool, a web app, a second server), update SPF first. Watch the DMARC reports for unknown sources; they are often the first sign that someone spoofs your domain.

Questions about SPF, DKIM and DMARC records

Do I need all three records?

Yes, for reliable delivery today. Large receivers expect SPF and DKIM on all mail and a DMARC record on the sending domain.

Can I have two SPF records?

No. Two v=spf1 records on one name make SPF fail. Merge every sender into one record.

Where do I set reverse DNS?

Reverse DNS (PTR) is not in the console. Open a support ticket with the IP addresses and the hostname you want.

Will these records fix a closed port 25?

No. DNS records do not open ports. Ask support by ticket about outbound 25, and send through an authenticated relay on 587 when it is closed.

Why does DKIM fail after I changed DNS?

Usually the record still has quotes or a line break inside the key, or the TTL has not expired yet. Run opendkim-testkey again after the TTL passes.

Is 1024 bit DKIM still acceptable?

Use 2048 bits. Most DNS providers accept the longer value, and receivers treat short keys with growing suspicion.