1Use a subdomain, not your root domain
Before clicking anything, decide the domain name. Mailgun's own recommendation — and the setup that avoids every conflict — is a dedicated subdomain such as mg.example.com. Your root domain almost certainly already has an SPF record and MX records for your real mailbox provider; adding Mailgun's on top of those means merging SPF strings and fighting MX priorities.
A subdomain gets its own clean SPF, its own DKIM, and its own MX records that can't collide with anything. Recipients still see mail from [email protected] if you set the From address that way — the subdomain is the envelope and signing infrastructure, not the visible address.
2Add the domain in the Mailgun dashboard
Log in at app.mailgun.com, go to Send → Sending → Domains, and click Add new domain. Enter mg.example.com, pick your region (US or EU — this is permanent and determines the API endpoint, so EU-based data residency needs the EU region), and create it with a 2048-bit DKIM key, which is the default.
3Copy the generated records
Mailgun shows the exact records for your domain: two TXT records (SPF and DKIM), two MX records for receiving bounces and replies at the subdomain, and a CNAME for open/click tracking. Copy them from your dashboard rather than from anyone's example — the DKIM selector and host values are specific to your domain.
mg.example.com TXT v=spf1 include:mailgun.org ~all
smtp._domainkey.mg.... TXT k=rsa; p=MIIBIjANBg... (your key)
mg.example.com MX 10 mxa.mailgun.org
mg.example.com MX 10 mxb.mailgun.org
email.mg.example.com CNAME mailgun.org
4Paste them into your DNS host
Add each record at your DNS provider. Watch the host fields: most panels append your domain automatically, so the SPF TXT goes at host mg (not mg.example.com.example.com), and the DKIM at smtp._domainkey.mg. On Cloudflare, set the tracking CNAME to DNS only (grey cloud) — proxied tracking links break open and click recording.
The MX records on the subdomain are safe to add even though you have MX on the root; they only govern mail sent to @mg.example.com addresses, which is exactly the bounce traffic Mailgun needs to see.
5Verify, then send a test
Back in Mailgun click Verify DNS settings. Records typically confirm within minutes; the dashboard shows a green check per record so you can see exactly which one is still pending. Once the domain state is Active, send a test message through the API or SMTP credentials and check the headers at the receiving end — you want spf=pass and dkim=pass with header.d=mg.example.com.
Finally, if you'll send at any volume, publish a DMARC record on the root domain if you haven't already — Mailgun's SPF and DKIM alignment on the subdomain will pass a relaxed-alignment DMARC policy out of the box.