OpenDMARC is an open source implementation of DMARC (Domain-based Message Authentication, Reporting and Conformance). DMARC builds on SPF and DKIM: a domain publishes a policy in DNS saying what receivers should do with mail that claims to be from it but fails both checks, and where to send reports about it.
DMARC has two sides, and it helps to keep them apart:
- Outgoing (your domain): you publish a
_dmarcDNS record. This needs no software at all, only working SPF and DKIM. - Incoming (your server): OpenDMARC checks mail arriving at your Postfix server against the sender’s DMARC policy, adds an
Authentication-Resultsheader, can reject failures, and can send aggregate reports back to other domains.
This guide covers both on Debian 12/13 (Ubuntu 22.04/24.04 works the same way). It assumes SPF and OpenDKIM are already set up as described in part 1, my OpenDKIM + Postfix guide, with OpenDKIM’s socket in /var/spool/postfix/opendkim/. Replace example.com with your domain and mail.example.com with your mail server’s hostname throughout.
This is part 2 of a two-part series: 1. DKIM signing with OpenDKIM · 2. DMARC with OpenDMARC (this post)
Step 1: Publish your DMARC record
Your DMARC record is a TXT record on _dmarc.example.com. Only add it once SPF and DKIM pass for all mail you send (Steps 8 and 10 of part 1); otherwise you are asking receivers to junk your own mail.
Start with monitoring only:
_dmarc.example.com. IN TXT "v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com"Code language: JavaScript (javascript)
Read the aggregate reports for two to four weeks. When every legitimate source of your mail passes, move to quarantine, then to the end goal:
_dmarc.example.com. IN TXT "v=DMARC1; p=reject; sp=reject; np=reject; adkim=r; aspf=r; rua=mailto:dmarc-reports@example.com"Code language: JavaScript (javascript)
The tags are separated by semicolons. v must come first; the order of the rest doesn’t matter.
v=DMARC1(required): protocol version.p(required): policy for your domain.none: take no action, only report. Use it while you are testing; it can’t break your mail.quarantine: treat failing mail as suspicious, usually by delivering it to spam.reject: refuse failing mail during the SMTP conversation. This is what actually stops people spoofing your domain.
sp: policy for subdomains such asshop.example.com. Same values asp; defaults top.np: policy for subdomains that don’t exist in DNS. Nobody sends legitimate mail from those, sonp=rejectis safe even whilepis stillnone.adkim/aspf: alignment mode for DKIM and SPF.r(relaxed, the default) lets a subdomain such asmail.example.comalign withexample.com;s(strict) requires an exact match. Relaxed is the right choice for almost everyone.rua: where receivers send daily aggregate reports (XML, usually zipped). Several addresses can be separated by commas. Withoutruayou get no reports and are flying blind.ruf: where to send per-message failure (forensic) reports. These can contain parts of real messages, so most large providers no longer send them. Leave it out unless you have a specific reason.fo: when to send failure reports (only matters withruf):0when both SPF and DKIM fail (default),1when either fails,don DKIM failure,son SPF failure.t=y: test mode. Receivers apply one level less than your policy, sop=reject; t=yacts like quarantine. Use it while stepping up your policy.
Tags you no longer need: pct, rf and ri. The current DMARC standard, RFC 9989 (May 2026, replacing RFC 7489), removed them: t=y replaces pct, and receivers send aggregate reports daily in one format whatever you ask for. Old records with these tags keep working, so there is no rush to change them.
Reports to another domain: if rua points to an address on a different domain (for example a reporting service), that domain must publish example.com._report._dmarc.otherdomain.net TXT "v=DMARC1", or receivers won’t send the reports. Reporting services set this up for you.
Reading reports: raw XML is hard to read. Free options include parsedmarc (self-hosted) or the free tiers of hosted DMARC report services.
Check the record:
$ dig +short TXT _dmarc.example.com
That’s the outgoing side done. The rest of this guide sets up OpenDMARC to check mail coming in.
Step 2: Install OpenDMARC
# apt update
# apt install opendmarc publicsuffixCode language: PHP (php)
publicsuffix provides the list OpenDMARC uses to work out a domain’s organizational domain (that mail.example.co.uk belongs to example.co.uk). The installer may ask whether to configure a database with dbconfig-common. Answer No for now; the database is only needed for sending reports (Step 7), and is easier to set up deliberately.
Step 3: Prepare the socket
As with OpenDKIM, the socket has to be inside the Postfix chroot, and Postfix needs to be in the opendmarc group to use it:
# mkdir -p /var/spool/postfix/opendmarc
# chown opendmarc:opendmarc /var/spool/postfix/opendmarc
# chmod 750 /var/spool/postfix/opendmarc
# usermod -aG opendmarc postfix
# mkdir -p /var/lib/opendmarc
# chown opendmarc:opendmarc /var/lib/opendmarcCode language: PHP (php)
Step 4: Configure OpenDMARC
Replace the contents of /etc/opendmarc.conf with:
# /etc/opendmarc.conf
# Full reference: man opendmarc.conf
## Run as the opendmarc user; socket is group-accessible for Postfix
UserID opendmarc
UMask 007
Socket local:/var/spool/postfix/opendmarc/opendmarc.sock
PidFile /run/opendmarc/opendmarc.pid
## Logging
Syslog true
## Name used in the Authentication-Results header this filter adds
AuthservID mail.example.com
## Authentication-Results headers from these IDs are trusted as input.
## Must include the AuthservID OpenDKIM uses, so DMARC can see the DKIM result.
TrustedAuthservIDs mail.example.com
## Don't evaluate mail we send ourselves
IgnoreHosts /etc/opendmarc/ignore.hosts
IgnoreAuthenticatedClients true
## No separate SPF daemon? Let OpenDMARC check SPF itself
SPFSelfValidate true
## Used to find the organizational domain
PublicSuffixList /usr/share/publicsuffix/public_suffix_list.dat
## What to do with failures
## RejectFailures: reject mail that fails DMARC (start with false)
## HoldQuarantinedMessages: put p=quarantine failures in Postfix's hold queue
RejectFailures false
HoldQuarantinedMessages false
## Reject mail with malformed headers (e.g. several From: headers)
RequiredHeaders true
## Don't send per-message failure reports (privacy; see Step 7)
FailureReports false
## Record results for aggregate reports (only needed for Step 7)
HistoryFile /var/lib/opendmarc/opendmarc.datCode language: PHP (php)
What the important options do:
- AuthservID: the name in the
Authentication-Resultsheader OpenDMARC adds. Use your mail server’s hostname. - TrustedAuthservIDs: OpenDMARC doesn’t check DKIM itself; it reads the
Authentication-Resultsheader OpenDKIM added. It only trusts headers carrying an ID in this list. If you run several mail servers that check mail before passing it on, list all their hostnames, separated by commas. - IgnoreHosts / IgnoreAuthenticatedClients: skip DMARC checks for mail from your own network and from users who log in with SMTP AUTH. That’s outgoing mail, which other servers will check.
- SPFSelfValidate: DMARC needs an SPF result. With this on, OpenDMARC checks SPF itself when no SPF result is present in the headers, so you don’t need a separate SPF policy daemon. If you already run
postfix-policyd-spf-python, you can leave it on; OpenDMARC uses the existing result. - RejectFailures: when
false, OpenDMARC only adds a header and your spam filter or mail client decides. Whentrue, mail that fails DMARC is rejected and mail whose check couldn’t complete (for example a DNS timeout) is temporarily refused, so the sender retries. Run withfalsefor a week or two and read the logs, then switch totrue. - HoldQuarantinedMessages: when
true, mail failing ap=quarantinepolicy goes into Postfix’s hold queue, where it sits until you release it withpostsuper -H. Most people prefer leaving thisfalseand letting the spam filter handle it. - RequiredHeaders: rejects messages that break the basic header rules, such as two
From:headers, a common trick to fool DMARC.
Match OpenDKIM’s ID: /etc/opendkim.conf from part 1 must contain the same name so both filters agree. If yours doesn’t have it yet, add it and restart OpenDKIM:
AuthservID mail.example.comCode language: CSS (css)
Create /etc/opendmarc/ignore.hosts with your own networks (the same hosts as OpenDKIM’s TrustedHosts):
# mkdir -p /etc/opendmarc
# nano /etc/opendmarc/ignore.hostsCode language: PHP (php)
127.0.0.1
::1
localhost
192.168.0.0/24
203.0.113.10
2001:db8::10
Start the service and check that the socket appears:
# systemctl enable --now opendmarc
# systemctl restart opendmarc
# systemctl status opendmarc
# ls -l /var/spool/postfix/opendmarc/Code language: PHP (php)
Upgraded from an old setup? As with OpenDKIM, the systemd service reads only /etc/opendmarc.conf. If the socket doesn’t appear where you expect, look for a leftover override in /etc/systemd/system/opendmarc.service.d/, remove it and run systemctl daemon-reload.
Step 5: Connect Postfix
In /etc/postfix/main.cf, add OpenDMARC after OpenDKIM. The order matters: OpenDKIM must check the signature first so OpenDMARC can read its result.
# DKIM signing/verification, then DMARC evaluation
milter_default_action = accept
smtpd_milters = local:opendkim/opendkim.sock, local:opendmarc/opendmarc.sock
non_smtpd_milters = $smtpd_miltersCode language: PHP (php)
# postfix check
# systemctl restart postfixCode language: PHP (php)
Step 6: Test
Incoming mail that should pass: send a message from a Gmail or Outlook account to your server and look at the headers. You should see something like:
Authentication-Results: mail.example.com; dkim=pass ... header.d=gmail.com
Authentication-Results: mail.example.com; spf=pass smtp.mailfrom=gmail.com
Authentication-Results: mail.example.com; dmarc=pass (p=none dis=none) header.from=gmail.comCode language: HTTP (http)
The mail log shows one line per evaluated message:
# journalctl -u opendmarc -f
# grep opendmarc /var/log/mail.logCode language: PHP (php)
Mail that should fail: from a machine that is not your mail server and not listed in your SPF record, send a message with your own domain in the From: header. swaks makes this easy:
$ swaks --server mail.example.com --to you@example.com --from test@example.com --header "Subject: DMARC fail test"Code language: JavaScript (javascript)
With RejectFailures false the message arrives with dmarc=fail in its headers. With RejectFailures true and p=reject on your domain, swaks shows the rejection (550 5.7.1 rejected by DMARC policy for example.com). Note that many home internet connections block outgoing port 25, so run this from a VPS or another server.
If DMARC always says dkim=none or fails for mail that is signed: OpenDMARC isn’t trusting OpenDKIM’s header. Compare the name after Authentication-Results: in the DKIM line with TrustedAuthservIDs; they must match exactly.
Mailing lists: some mailing lists change the subject or footer, which breaks the original DKIM signature, so mail from a p=reject domain sent through such a list fails DMARC. Most modern list software rewrites the From: address to avoid this, but keep an eye on the logs before turning on RejectFailures.
Step 7 (optional): Send aggregate reports
Your server is now fully working. Sending aggregate reports back to the domains that mail you is a courtesy to them, not a requirement; plenty of small servers skip it. If you want to, OpenDMARC writes every result to the HistoryFile, and a daily job imports it into MariaDB and sends the reports.
Don’t send per-message failure (forensic) reports. They can contain parts of your users’ messages, for example from mailing list posts, so it’s a privacy risk with little benefit. That’s why FailureReports is false above.
Create the database
Install MariaDB (or use an existing MySQL/MariaDB server) and the Perl database driver the report tools need:
# apt install mariadb-server libdbd-mysql-perlCode language: PHP (php)
Create the database and a user. Pick a long random password, for example with openssl rand -base64 24:
# mariadb
CREATE DATABASE opendmarc;
CREATE USER 'opendmarc'@'localhost' IDENTIFIED BY 'your-long-random-password';
GRANT ALL PRIVILEGES ON opendmarc.* TO 'opendmarc'@'localhost';
FLUSH PRIVILEGES;
EXIT;Code language: PHP (php)
Load the schema that ships with the package:
# mariadb opendmarc < /usr/share/doc/opendmarc/schema.mysqlCode language: PHP (php)
If the file is compressed on your system (schema.mysql.gz), use zcat /usr/share/doc/opendmarc/schema.mysql.gz | mariadb opendmarc instead.
Store the credentials
The import and report tools read their database settings from environment variables, which keeps the password out of the script and out of the process list. Create /etc/opendmarc/report.env:
OPENDMARC_DBHOST=localhost
OPENDMARC_DB=opendmarc
OPENDMARC_USER=opendmarc
OPENDMARC_PASSWORD=your-long-random-password
REPORT_EMAIL=dmarc-noreply@example.com
REPORT_ORG=example.com
# chown root:opendmarc /etc/opendmarc/report.env
# chmod 640 /etc/opendmarc/report.envCode language: PHP (php)
REPORT_EMAIL is the From: address of the reports you send. Use an address on your domain that can receive bounces; the reports go out through your own Postfix and are DKIM-signed like any other mail.
The report script
Create /usr/local/sbin/opendmarc-report:
#!/bin/bash
# Import OpenDMARC history into the database, send aggregate reports,
# and expire old data. Runs as the opendmarc user.
set -euo pipefail
set -a
. /etc/opendmarc/report.env
set +a
HISTORY=/var/lib/opendmarc/opendmarc.dat
SPOOL=/var/lib/opendmarc
# Move the current history aside; OpenDMARC starts a new file on the next message
if [ -s "$HISTORY" ]; then
mv "$HISTORY" "$SPOOL/import-$(date +%Y%m%d%H%M%S).dat"
fi
# Import every pending file; a file is only deleted after a successful import,
# so nothing is lost if the database was unreachable last time
for f in "$SPOOL"/import-*.dat; do
[ -e "$f" ] || continue
/usr/sbin/opendmarc-import --input="$f"
rm -f "$f"
done
# Send yesterday's reports
/usr/sbin/opendmarc-reports --day \
--report-email="$REPORT_EMAIL" \
--report-org="$REPORT_ORG"
# Keep 180 days of data (opendmarc-expire does not read the environment variables)
/usr/sbin/opendmarc-expire --expire=180 \
--dbhost="$OPENDMARC_DBHOST" --dbname="$OPENDMARC_DB" \
--dbuser="$OPENDMARC_USER" --dbpasswd="$OPENDMARC_PASSWORD"Code language: PHP (php)
# chmod 755 /usr/local/sbin/opendmarc-reportCode language: PHP (php)
Run it once by hand as the opendmarc user, with --verbose added to the commands if you want to see what happens:
# runuser -u opendmarc -- /usr/local/sbin/opendmarc-reportCode language: PHP (php)
Run it daily with a systemd timer
Create /etc/systemd/system/opendmarc-report.service:
[Unit]
Description=Send OpenDMARC aggregate reports
After=network-online.target mariadb.service postfix.service
[Service]
Type=oneshot
User=opendmarc
Group=opendmarc
ExecStart=/usr/local/sbin/opendmarc-reportCode language: JavaScript (javascript)
And /etc/systemd/system/opendmarc-report.timer:
[Unit]
Description=Daily OpenDMARC aggregate reports
[Timer]
OnCalendar=*-*-* 01:15:00
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.targetCode language: JavaScript (javascript)
# systemctl daemon-reload
# systemctl enable --now opendmarc-report.timer
# systemctl list-timers opendmarc-report.timerCode language: PHP (php)
Output of each run goes to the journal: journalctl -u opendmarc-report.
Notes
- Organizational domain: RFC 9989 finds a domain’s organizational domain by walking up the DNS tree instead of using the Public Suffix List. OpenDMARC 1.4 still uses the list. For almost every domain the result is the same; just keep the
publicsuffixpackage updated. - Alternatives: if you’d rather run one daemon than several milters, rspamd handles DKIM signing and verification, SPF, DMARC (including reports, stored in Redis), ARC and spam filtering together.