How do you set up DMARC for Mailgun?
Mailgun DMARC passes when the From address uses the domain you verified in Mailgun, or its parent under relaxed alignment. Add the sending domain (Mailgun suggests a subdomain such as mg.example.com), publish its SPF and DKIM records, and keep the From domain matched. Mailgun signs with that domain and uses it as the bounce address, so both checks align.
This guide explains what Mailgun's records do for DMARC (Domain-based Message Authentication, Reporting and Conformance), how SPF (Sender Policy Framework) and DKIM (DomainKeys Identified Mail) align when the From address is the root domain or the subdomain, and how Automatic Sender Security rotates keys. It ends with a failure table and an MSP (managed service provider) checklist. For protocol background, see the DMARC guide.
Key takeaways
- Mailgun recommends a subdomain such as
mg.example.comormail.example.comwhen the root domain already receives mail elsewhere. The subdomain becomes the DKIM domain and the envelope (bounce) domain. - A From address on the sending subdomain aligns under both relaxed and strict alignment. A From address on the root domain aligns only under relaxed alignment, which is the default in RFC 9989.
- Mailgun does not create or check DMARC records. You publish and manage the policy yourself.
- Automatic Sender Security replaces the DKIM TXT record with two CNAMEs (
pdk1andpdk2) and rotates 2048-bit keys every 120 days by default. - A sending subdomain without its own DMARC record inherits the parent domain's
sp=policy, so publish one atp=nonewhile you test.
Should you send from a subdomain or the root domain?
Use a subdomain in almost every client setup. Mailgun's getting started FAQ says that if you receive email elsewhere on the domain, you should use a subdomain at Mailgun, and it suggests names like m.mycompany.com or mail.mycompany.com. The root domain is only an option when it is not used for corporate email.
The reason is the MX record. A client on Microsoft 365 or Google Workspace already points the root domain's MX at its mailbox provider, and Mailgun's DNS FAQ warns that pointing those MX records at Mailgun breaks existing inbound mail. A subdomain gives Mailgun its own namespace for SPF, DKIM, MX and tracking without touching the client's main mail flow. It also keeps the root SPF record inside its 10-lookup budget (see SPF too many DNS lookups).
A subdomain is an authentication boundary, not a reputation firewall. Subdomain or separate domain for bulk email covers how mailbox providers treat subdomain reputation and bulk thresholds.
Which DNS records does Mailgun ask for?
Mailgun asks for two TXT records to send (SPF and DKIM), two MX records to receive, and an optional tracking CNAME. Its domain verification setup guide marks SPF and DKIM as required, MX as required for receiving and the tracking CNAME as highly recommended. The DKIM value is unique per domain, so copy it from the control panel.
For a US region domain, using mg.example.com as the sending domain, the records look like this. The SPF, MX and CNAME values come from Mailgun's sending domain documentation:
mg.example.com. TXT "v=spf1 include:mailgun.org ~all"
mx._domainkey.mg.example.com. TXT "k=rsa; p=MIIBIjANBgkqh...(value from the Mailgun control panel)"
mg.example.com. MX 10 mxa.mailgun.org.
mg.example.com. MX 10 mxb.mailgun.org.
email.mg.example.com. CNAME mailgun.org.
| Record | Host | Purpose | Needed for DMARC? |
|---|---|---|---|
| SPF TXT | mg.example.com |
Authorizes Mailgun's servers for the envelope domain | Yes, for aligned SPF |
| DKIM TXT | <selector>._domainkey.mg.example.com |
Publishes the public key Mailgun signs with | Yes, for aligned DKIM |
| MX (x2) | mg.example.com |
Receives bounces and inbound mail on the subdomain | Indirectly: avoids sender verify rejections |
| Tracking CNAME | email.mg.example.com |
Rewrites open, click and unsubscribe links | No |
Two details trip people up. First, the DKIM selector is not fixed: Mailgun's setup guide uses mx._domainkey as its example, while domains using Automatic Sender Security use pdk1 and pdk2, so always read the selector from the control panel. Second, the tracking hostname prefix defaults to email, per the web_prefix parameter in Mailgun's create a domain API reference.
What changes for the EU region?
The MX and tracking targets change. Mailgun's region transfer article lists mxa.eu.mailgun.org and mxb.eu.mailgun.org for MX, eu.mailgun.org for tracking, api.eu.mailgun.net for the API and smtp.eu.mailgun.org for SMTP. A domain's region cannot be changed after creation, so pick the region before you publish records.
For UK and EU clients with data residency requirements, decide this during onboarding. Moving later means adding a new domain in the EU region or deleting and recreating the domain, then exporting suppressions and rebuilding routes and webhooks. The DNS records for DMARC change with it.
Do you add mailgun.org to the root SPF record?
Only if the root domain itself is the Mailgun sending domain. SPF (RFC 7208) checks the envelope (MAIL FROM) domain, and with a sending subdomain that is mg.example.com, not the root. In that case include:mailgun.org belongs on the subdomain's SPF record, and adding it to the root only spends a DNS lookup. If you do send from the root, merge the include into the existing record: one SPF record per hostname.
What domain does Mailgun use for the return path?
The sending domain. Mailgun's sender verify article shows envelope addresses such as bounce+c72392.3559d@yoursendingdomain.com, with the sending domain after the @. So the Return-Path of a message sent through mg.example.com is an address at mg.example.com, and SPF is evaluated against that subdomain.
That is why the MX records matter even if the client never receives mail through Mailgun. Some receiving servers check that the envelope domain has MX records and reject with "Sender verify failed" if it does not. Mailgun's fix is to point the subdomain's MX at mxa.mailgun.org and mxb.mailgun.org (or the EU equivalents). Mailgun's verification docs do not describe a separate return path record: the envelope domain follows the verified sending domain, so choosing that domain is the decision that matters for SPF alignment.
How does Mailgun DMARC alignment work with relaxed and strict modes?
DMARC passes when SPF or DKIM passes for a domain aligned with the From domain. Under RFC 9989, relaxed alignment (r, the default for both adkim and aspf) accepts any domain sharing the same organizational domain. Strict alignment (s) requires an exact match. With Mailgun, the result depends on which domain appears in the From address.
| From address | DKIM d= |
Envelope domain | Relaxed (adkim=r; aspf=r) |
Strict (adkim=s; aspf=s) |
|---|---|---|---|---|
news@mg.example.com |
mg.example.com |
mg.example.com |
Pass (DKIM and SPF align) | Pass (DKIM and SPF align) |
news@example.com |
mg.example.com |
mg.example.com |
Pass (DKIM and SPF align) | Fail (neither aligns) |
news@example.com, root domain verified in Mailgun |
example.com |
example.com |
Pass | Pass |
news@client.co.uk (not verified in the account) |
mg.example.com |
mg.example.com |
Fail | Fail |
Mailgun's own DMARC article states the same rule: the From domain must match the Mailgun sending (sub)domain or its root domain, and you should not use the root domain in the From address if the DMARC record sets strict SPF alignment. The full mechanics are in DMARC alignment explained: relaxed vs strict.
Can a subdomain sign with the root domain's DKIM key?
Mailgun documents a DKIM authority setting for this. Its update DKIM authority reference says a domain is its own authority by default, and that setting self to false gives it the same DKIM authority as the root domain registered on the same account. If a client needs strict DKIM alignment with a root domain From address, test this on a non-production domain and confirm the d= value in real headers before relying on it.
Why do Outlook users see "on behalf of"?
Because the envelope domain and the From domain differ. Mailgun's "on behalf of" article explains that the notation appears when mail goes through mail.example.com but claims to come from example.com. The recommended fix is a From address on the sending domain itself. It is a display issue, not a DMARC failure, but clients notice it.
How does Mailgun rotate DKIM keys?
With Automatic Sender Security, Mailgun rotates keys for you. Its DKIM security documentation says you publish two CNAMEs at selectors pdk1 and pdk2, pointing at Mailgun-hosted 2048-bit keys. Keys rotate every 120 days by default, adjustable to a minimum of 5 days, and two keys stay active during rotation.
pdk1._domainkey.mg.example.com. CNAME pdk1._domainkey.<value>.dkim1.mailgun.com.
pdk2._domainkey.mg.example.com. CNAME pdk2._domainkey.<value>.dkim1.mailgun.com.
Without it, you manage TXT keys yourself. Mailgun allows up to three active DKIM keys per sending domain, signs in round robin across them, supports 1024-bit or 2048-bit generated keys and recommends rotating at least every six months. Its setup guide also notes that enabling Automatic Sender Security deactivates active manual keys once the CNAMEs verify. For a portfolio of clients, CNAME delegation removes a recurring task. See how to rotate DKIM keys safely for the manual process.
How do you verify Mailgun authentication with dig and headers?
Check DNS first, then a real message. Mailgun's FAQ confirms you can verify records with dig, and the control panel has a "Check DNS Records Now" button. Replace the selector with the one Mailgun shows for the domain:
dig +short TXT mg.example.com
dig +short TXT mx._domainkey.mg.example.com
dig +short CNAME pdk1._domainkey.mg.example.com
dig +short MX mg.example.com
dig +short TXT _dmarc.mg.example.com
dig +short TXT _dmarc.example.com
On Windows, Resolve-DnsName -Type TXT mg.example.com does the same. Then send a test to a Gmail or Outlook.com mailbox and read the Authentication-Results header:
Authentication-Results: mx.google.com;
dkim=pass header.i=@mg.example.com header.s=pdk1;
spf=pass smtp.mailfrom="bounce+1a2b3c@mg.example.com";
dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=mg.example.com
You want dkim=pass with header.i or d= on the sending domain, spf=pass with smtp.mailfrom on the same domain, and dmarc=pass. The SPF analyzer checks the subdomain's SPF record and lookup count, and the email header analyzer parses the results.
Why does Mailgun mail fail DMARC?
Most failures come from a From domain that does not match the verified sending domain, strict alignment with a root domain From address, or DNS records that never verified. Start from the aggregate reports, find the Mailgun source and compare the DKIM and envelope domains with the header From.
| Symptom | Likely cause | Fix |
|---|---|---|
DKIM passes for mg.example.com, DMARC fails |
From uses a domain not related to the sending domain | Send From the sending domain or its root, or verify that domain in Mailgun |
SPF and DKIM pass but DMARC fails, From is example.com |
Client DMARC record sets aspf=s and adkim=s |
Use relaxed alignment, or send From mg.example.com |
| DKIM TXT never verifies | A CNAME already exists at the hostname | Remove the CNAME or choose another sending subdomain, as Mailgun's DNS FAQ advises |
Record shows as mg.example.com.example.com |
DNS panel appended the zone name | Enter only the host label, or a fully qualified name with a trailing dot |
| "Sender verify failed" bounces | No MX records on the sending subdomain | Add Mailgun's MX records to the subdomain |
| Inbound mail stops arriving at the root domain | Root MX pointed at Mailgun | Restore the mailbox provider's MX and move Mailgun to a subdomain |
| 2048-bit key rejected by DNS host | TXT string too long for the panel | Split the record into strings, or use Automatic Sender Security CNAMEs |
Mail rejected after parent goes to p=reject |
Subdomain has no own record and inherits sp=reject |
Publish _dmarc.mg.example.com at p=none until reports are clean |
Why does DMARC fail when SPF passes covers the general patterns behind several of these rows.
What do sp and np mean for the Mailgun subdomain?
A sending subdomain with no DMARC record of its own inherits the parent's subdomain policy. RFC 9989 has receivers walk up the DNS tree to the organizational domain and apply its sp= tag (or p= if sp= is absent). The np= tag covers only non-existent subdomains, and mg.example.com exists once it has TXT and MX records.
That has two practical effects. If the client's root record already says sp=reject, an unaligned Mailgun stream on a new subdomain is rejected from the first message. And np=reject does nothing for the Mailgun subdomain itself, but it does protect typo names nobody created. A safe pattern during onboarding:
_dmarc.example.com. TXT "v=DMARC1; p=reject; sp=reject; np=reject; rua=mailto:dmarc@example.com"
_dmarc.mg.example.com. TXT "v=DMARC1; p=none; rua=mailto:dmarc@example.com"
The subdomain record wins over the inherited policy while you test. Note that sp= is ignored on records published at subdomains. Once reports show aligned Mailgun traffic, move the subdomain to p=quarantine and then p=reject in full steps: RFC 9989 removed the percentage tag, so there is no partial ramp. The t=y testing flag is available if you want receivers to treat the policy as a test. See DMARC for subdomains and the new np, psd and t tags.
What should an MSP check before enforcing a client that uses Mailgun?
Confirm ownership, records and real traffic before any policy change. Mailgun accounts are often created by a developer or agency, not by IT, so the first job is finding out who controls the account and which domains it sends for.
- Inventory: list every Mailgun sending domain and From address per client, including test and staging domains.
- Ownership: record who holds the Mailgun account, which region each domain uses and who can change DNS.
- Records: confirm SPF, DKIM (or both
pdkCNAMEs) and MX verify for each sending domain; check the tracking CNAME if tracking is on. - Alignment: confirm the client's
adkimandaspfsettings match the From domains in use; prefer relaxed unless every From address is on the sending subdomain. - Subdomain policy: publish
_dmarc.mg.example.comatp=noneif the parent already carriessp=quarantineorsp=reject. - Reports: watch aggregate reports for a full sending cycle, including monthly invoices and password resets, before moving to
p=quarantine. - Offboarding: when a client leaves Mailgun, remove the subdomain's records and delete the domain in Mailgun, so no orphaned DKIM keys or MX records remain.
DMARC AI groups each client's aggregate report sources across the portfolio, so a Mailgun stream with unaligned From addresses stands out before enforcement. The UK NCSC's email security and anti-spoofing guidance also recommends starting at a policy of none. For the wider vendor process, see how to handle third-party senders, and compare the SendGrid DMARC guide and the Amazon SES guide.
FAQ
Does Mailgun create a DMARC record for my domain?
No. Mailgun's DMARC article says it neither offers DMARC records nor checks for their presence. You publish the record at _dmarc for the organizational domain, and optionally for the sending subdomain. The DMARC generator builds a starting record at p=none with an aggregate report address.
Which DKIM selector does Mailgun use?
It depends on the domain. Mailgun's setup guide uses mx._domainkey as the example TXT host, and Automatic Sender Security uses the pdk1 and pdk2 selectors as CNAMEs. A selector can also be set through the API when a domain is created, so always copy it from the control panel.
Can I send From the root domain if Mailgun uses mg.example.com?
Yes, under relaxed alignment. DKIM signs with mg.example.com and the envelope uses it too, and both share the organizational domain example.com. Under strict alignment neither aligns, so DMARC fails. Outlook may also show "on behalf of", which Mailgun fixes by sending From the sending domain.
Do I need Mailgun's MX records if I only send mail?
Not for verification: Mailgun's DNS FAQ says the two TXT records are all that is required to send. In practice, add them to a sending subdomain anyway, because some receivers reject mail whose envelope domain has no MX records. Never point a root domain's MX at Mailgun if it receives mail elsewhere.
Final thoughts
Mailgun and DMARC fit together cleanly once each client sends from a verified subdomain with the From address on that subdomain or its parent. Keep alignment relaxed unless the From domain matches exactly, use Automatic Sender Security to take key rotation off your list, give the subdomain its own DMARC record while testing, and let the aggregate reports decide when to enforce.