DMARC, SPF and DKIM for Mailgun

Updated 2026-11-14

Mailgun is an email API for transactional and bulk mail. You add a domain, publish the records the control panel shows and wait for verification. DMARC you publish yourself. This guide follows Mailgun's documentation. For background on each standard, see SPF explained, DKIM explained and DMARC explained.

What Mailgun sends from

The visible From address is yours, for example [email protected]. Mailgun's FAQ says you can technically set From to anything and recommends using the same domain as the sender.

The envelope sender (Return-Path, also called MAIL FROM) is what the FAQ calls the sender: "The sender must always be one of your Mailgun domains." By default the mail-from host is the domain you added. The Domains API says: "The mail-from host will remain the domain name being created, not the root domain." The update endpoint has a mailfrom_host setting; in our reading, changing it moves SPF and SPF alignment to the new host. Mailgun does not print a sample Return-Path header, so check the headers of a test mail.

Which domain should you add? Mailgun's FAQ recommends your root domain "unless you're already using this name for your corporate email", and otherwise a subdomain such as m.mycompany.com or mail.mycompany.com. Its DNS best practices suggest the top level domain for corporate mail and "different domains or subdomains for your marketing and transactional mail". This guide uses mail.example.com. A separate FAQ page adds that a completely separate domain "(not a subdomain of your primary corporate domain)" may be safer if you worry about domain reputation.

Region: you "Select Domain region" when adding a domain, and the API has US (api.mailgun.net) and EU (api.eu.mailgun.net) endpoints. The verification page prints no regional variants, so copy the records your control panel shows.

Unverified domains are limited to 300 emails per day, and the sandbox domain under mailgun.org is for testing only.

SPF

As of November 2026 the verification table lists this SPF value as required:

mail.example.com TXT v=spf1 include:mailgun.org ~all

The table does not name the host. Since SPF is checked on the envelope domain, we read it as belonging on the mail-from host: mail.example.com in this guide, or example.com if you added the root.

This record uses 1 DNS lookup in the record itself. Mailgun does not document how many nested lookups include:mailgun.org uses. RFC 7208 sets the "overall limit of 10" for the whole tree, and the SPF lookup counter counts it. See also too many SPF lookups.

If you added your root domain and it already has an SPF record, do not add a second one. Mailgun's FAQ says you "should NOT set up a separate TXT record for each" server and shows a merged record:

example.com TXT v=spf1 include:myemailserver.com include:mailgun.org ~all

include:myemailserver.com is Mailgun's placeholder. This record uses 2 lookups itself, plus each include's nested lookups (multiple SPF records).

If you send from mail.example.com, the root record does not need include:mailgun.org for Mailgun mail, in our reading.

The same table also marks MX (10 mxa.mailgun.org, 10 mxb.mailgun.org) and a tracking CNAME as required. In our reading you need the MX records only to receive mail at Mailgun and the CNAME only if you use open and click tracking, but the FAQ says "It is recommended for all domains to use MX records".

DKIM

Mailgun offers two ways to publish DKIM, and its pages do not agree on every detail.

Manual keys (TXT). The verification table lists DKIM as a required TXT record and says to find it in the "Domain Verification & DNS" section of the control panel. The Domain Keys API example returns this record shape:

s1._domainkey.example.com TXT k=rsa; p=<public_key>

s1 is an example. You choose the selector, and the docs say it must be "unique to the sending domain". As of November 2026 the key length is 1024 or 2048 bits. Mailgun calls 2048 "more secure" and warns that some DNS providers need the long record split into two parts. The API's use_automatic_sender_security setting "Defaults to false"; switching it on later means the domain "must be reverified".

Automatic Sender Security (CNAME). Mailgun hosts the key and rotates it. The DKIM Security page shows two CNAMEs, using my.domain.com as the customer domain:

pdk1._domainkey.my.domain.com CNAME pdk1._domainkey.9d876.dkim1.mailgun.com
pdk2._domainkey.my.domain.com CNAME pdk2._domainkey.9d876.dkim1.mailgun.com

Copy your own values. The same page then says the feature "generates two 2048 bit DKIM selector records via TXT records". In our reading the keys are TXT records on Mailgun's side and your zone holds only the CNAMEs, which is what the API text describes: "DNS CNAME entries for DKIM keys instead of a TXT record".

Rotation: Mailgun recommends "rotating your DKIM key at least every 6 months". With Automatic Sender Security the default is every 120 days, adjustable, with a minimum of 5 days. For manual rotation, add a key with a new selector, send test mail to confirm the new key signs, then delete the old one. Mailgun signs with up to 3 active keys in round robin, so in our reading every active key should be in DNS. The verification page adds that unverified keys may be listed and "It is not required for all DKIM keys to be verified" as long as the key the domain's configuration needs is in DNS.

Signing domain: the docs do not print a sample DKIM-Signature header, and they disagree on the default DKIM authority. The create-domain API says that with force_dkim_authority false (the default) a subdomain gets "the same DKIM authority as the root domain registered on the same mailgun account". The DKIM authority endpoint says "Domain's authority is set to itself by default." So if example.com is on the same account, d= may be example.com by default rather than mail.example.com. Check a test mail.

DMARC alignment

DMARC passes when SPF or DKIM passes and that domain aligns with the From domain. Relaxed alignment means the same organisational domain; strict means identical domains.

  • SPF alignment compares the Return-Path domain with From. With From on mail.example.com and the Mailgun domain mail.example.com, they are identical and align under strict and relaxed. With From on example.com and Mailgun on mail.example.com, SPF aligns only under relaxed alignment (aspf=r, the default).
  • DKIM alignment compares the signature's d= with From. With the subdomain as its own DKIM authority, d= should match it. If the root is the authority, which may be the default when the root is on the same account, d=example.com aligns with mail.example.com only under relaxed alignment. That d= becomes the root is our reading of the API text.

Keep relaxed alignment unless a test mail shows identical domains. In general, not specific to Mailgun, DKIM usually survives forwarding and SPF often does not.

Step by step to p=reject

  1. Add the sending domain. In the control panel click Send, then Sending, then Domains, then Add new domain. Enter mail.example.com (or your root), select the domain region, choose the IP assignment option and, if offered, Automatic Sender Security, then click Add Domain and answer the verification email Mailgun sends.
  2. Publish SPF and DKIM. Add the SPF TXT record with include:mailgun.org on that domain and the DKIM record exactly as shown, as a TXT key or as the two CNAMEs for Automatic Sender Security. Add MX only if you receive mail at Mailgun, and the tracking CNAME if you use tracking.
  3. Verify the domain. Mailgun says DNS can take 24-48 hours. Wait for the automatic check or click Verify DNS settings, until the domain shows a green Verified badge.
  4. Publish DMARC with p=none and reports. Add a TXT record at _dmarc.example.com with v=DMARC1; p=none; rua=mailto:[email protected]. Delivery does not change; receivers start sending aggregate reports.
  5. Read the reports and fix other senders. For a few weeks, check that Mailgun mail passes aligned DKIM and SPF, and give every other legitimate sender aligned DKIM or SPF.
  6. Move to p=quarantine. Change the policy to p=quarantine and keep reading reports, because a sender you missed now lands in spam.
  7. Move to p=reject. When the reports show nothing legitimate failing, change the policy to p=reject.

The record for step 4:

_dmarc.example.com TXT v=DMARC1; p=none; rua=mailto:[email protected]

That record triggers our DMARC policy none note on purpose. The final record:

_dmarc.example.com TXT v=DMARC1; p=reject; rua=mailto:[email protected]

A record on _dmarc.example.com also covers mail.example.com, unless mail.example.com has its own _dmarc record. Some receivers support pct (RFC 7489) or t=y (RFC 9989), but RFC 7489 says "unknown tags MUST be ignored", so a receiver may apply p=reject; t=y as a full reject. Do not treat either tag as a safety net (partial pct).

Common mistakes

  • Using -all in SPF. RFC 9989 warns that -all can get mail "rejected early in the SMTP transaction, before any DMARC processing takes place", even with aligned DKIM. Mailgun's own value uses ~all; keep it.
  • SPF on the wrong name. Put include:mailgun.org on the domain you added to Mailgun, not only on the root.
  • A second SPF record. Merge includes into one record per name.
  • Missing the DKIM key Mailgun signs with. Publish the key or CNAMEs your configuration needs; in our reading an active key missing from DNS can fail DKIM for some mail (DKIM not found).
  • MX on a domain that receives elsewhere. Mailgun recommends MX records for all domains, but only one server can receive for a domain; the FAQ says to skip the Mailgun MX if you do not receive there. A dedicated subdomain avoids the conflict.
  • No report address. Without rua you cannot see what fails (DMARC without rua).

How to check it

Run the email security check on your sending domain and on example.com. Use the SPF lookup counter on the domain that holds include:mailgun.org to count the full tree. Mailgun selectors vary: enter pdk1 and pdk2 for Automatic Sender Security, or the selector you chose, in the DKIM selector finder. Build your policy with the DMARC record generator.

Frequently asked questions

Where does the Mailgun SPF record go?

On the mail-from host, which by default is the domain you added to Mailgun. As of November 2026 the verification table lists a TXT record with v=spf1 include:mailgun.org ~all. If you added mail.example.com, SPF is checked there, not on example.com. The Domains API has a mailfrom_host setting; in our reading, changing it moves SPF and SPF alignment to the new host. If you added example.com itself, merge the include into your existing root record.

Is the Mailgun DKIM record a TXT or a CNAME?

It depends on Automatic Sender Security. With it off, DKIM is a TXT record holding the key, such as s1._domainkey.example.com in the API example. With it on, the docs describe two CNAMEs, pdk1._domainkey and pdk2._domainkey, pointing to names under dkim1.mailgun.com. Mailgun's pages are not consistent on this point, so publish exactly what your control panel shows.

Should I send from a subdomain?

Mailgun suggests it if your root domain already handles corporate mail. Its FAQ recommends names like m.mycompany.com or mail.mycompany.com, and its DNS best practices suggest separate subdomains for marketing and transactional mail. A subdomain keeps SPF apart and may help separate reputation, but SPF then aligns with a root From only under relaxed alignment. Another Mailgun FAQ says a completely separate domain, not a subdomain, may be safer if you worry about domain reputation.

Does Mailgun publish DMARC for me?

No. Mailgun lists DMARC as something you implement, and the setup pages we read print no DMARC record. You publish a TXT record at _dmarc.example.com yourself. Start with p=none and a rua address, then move to quarantine and reject.

Sources