Mailgun: SPF, DKIM and DMARC setup
Your app sends through Mailgun, and a receiver reports an SPF, DKIM or DMARC failure. Mailgun sends from a domain you add and verify, often a subdomain such as mail.example.com. Here are its records, and when that subdomain aligns with your From address.
The DNS records Mailgun asks for
Add the domain under Sending, Domains, then copy the records Mailgun shows. The examples use the sending domain mail.example.com.
- TXT
Name
mail.example.comValuev=spf1 include:mailgun.org ~allSPF for the sending domain. - CNAME
Name
pdk1._domainkey.Valuemail.example.com pdk1._domainkey.<id>.dkim1.mailgun.comDKIM with Automatic Sender Security. A second record does the same forpdk2. Mailgun hosts the keys and rotates them. - TXT
Name
<selector>._domainkey.Valuemail.example.com <DKIM key from Mailgun>DKIM without Automatic Sender Security: one TXT key instead of the two CNAMEs. - CNAME
Name
email.mail.example.comValuemailgun.orgClick, open and unsubscribe tracking. It does not affect SPF, DKIM or DMARC. - MX
Name
mail.example.comValue10 mxa.mailgun.org10 mxb.mailgun.orgFor Mailgun to receive mail for the sending domain. MX records decide where all mail for that name goes, so don't put them on a name your mailboxes use.
Which check aligns with DMARC
- Yes: SPF aligns in relaxed mode. The Return-Path is on your Mailgun sending domain. With relaxed alignment, the DMARC default,
mail.example.comaligns with From addresses atmail.example.comorexample.com. - Careful: Strict alignment breaks root From addresses. With
aspf=s, mail from[email protected]sent throughmail.example.comfails SPF alignment. Mailgun says to useaspf=r. The same rule applies to DKIM andadkim=s. - Yes: DKIM aligns once the DKIM record for your sending domain is verified, under the same relaxed rule.
DKIM alignment alone is enough for DMARC to pass.
Common mistakes
- Two SPF records. If you send from the root domain and it already has SPF, add
include:mailgun.orgto that record. Don't add a second one. Then count the lookups: each include adds to the 10-lookup limit. - A From domain that doesn't match. Mailgun asks that the From address uses the Mailgun sending domain or its root domain. A From address on another domain can't align.
- Unverified extra DKIM keys. Mailgun may list older or rotated keys as unverified. Only the key in use must be in DNS.
- The DNS host adds your domain twice. Some hosts want only the part before your zone, such as
mailinstead ofmail.example.com. - Cloudflare proxy. Keep the DKIM CNAMEs DNS only (grey cloud). Cloudflare recommends DNS only for email and domain verification records.
Check your setup
- Check your domain with MailDoctor (form below) for SPF, DMARC and the rest.
- Check the key with the DKIM key checker on your sending domain: selector
pdk1andpdk2with Automatic Sender Security, or the selector Mailgun shows. - Check the sending domain's SPF record, and your root record if you changed it, with the SPF lookup counter.
- Send yourself a message through Mailgun and paste its headers into the email header analyzer. It shows the Return-Path, the DKIM domain, and which of them aligns.
Marketing mail: Mailgun asks bulk senders to have one-click unsubscribe enabled. See Mailgun's DMARC article.
Sources, checked 27 November 2026: Domain verification · Domain verification setup guide · DKIM security · DMARC · Cloudflare proxy status
Check the domain you send from. Grade, problems and the exact records to fix them.