Mailgun Email Authentication Setup

Sinch Mailgun is a transactional email API service for developers. This guide covers the complete email authentication stack for Mailgun: SPF, DKIM, and DMARC. Each section gives you the exact DNS records, step-by-step instructions, common pitfalls, and how to verify your setup.

How to Set Up SPF for Mailgun

Authorize Mailgun to send transactional email on behalf of your domain by adding the correct SPF include.

Quick Answer — The Record You Need

Type

TXT

Host / Name

@

Value
v=spf1 include:mailgun.org ~all

Add include:mailgun.org to your existing SPF record.

Step-by-Step Setup

1

Log in to Mailgun

Go to app.mailgun.com and sign in.

2

Add your domain

Navigate to Sending > Domains > Add New Domain. Enter your domain (or a subdomain like mg.yourdomain.com).

3

Get DNS records

Mailgun will display the DNS records you need. The SPF record uses include:mailgun.org.

4

Add the SPF record to your DNS

Add include:mailgun.org to your existing SPF record, or create a new one.

v=spf1 include:mailgun.org ~all
5

Verify in Mailgun

Click "Verify DNS Settings" in Mailgun. It will check all required records.

Before & After

Before
v=spf1 include:_spf.google.com ~all
After
v=spf1 include:_spf.google.com include:mailgun.org ~all

SPF Gotchas

  • Mailgun recommends using a subdomain (e.g., mg.yourdomain.com) for sending, which keeps your root domain SPF record clean.
  • The include is mailgun.org, not mailgun.com. Using the wrong domain will not authorize Mailgun servers.
  • If you use a subdomain for Mailgun, the SPF record goes on that subdomain, not your root domain.

SPF FAQ

What SPF record do I need for Mailgun?

Add include:mailgun.org to your SPF record. If you send from a subdomain like mg.yourdomain.com, add the SPF record to that subdomain.

Should I use a subdomain with Mailgun?

Yes. Mailgun recommends using a subdomain (e.g., mg.yourdomain.com) to isolate transactional email reputation from your root domain.

Can I use Mailgun and Google Workspace on the same domain?

Yes. Use a subdomain for Mailgun (e.g., mg.yourdomain.com with its own SPF) and keep your root domain SPF for Google Workspace.

How to Set Up DKIM for Mailgun

Publish a DKIM TXT record provided by Mailgun to enable cryptographic email signing for your sending domain.

Quick Answer — The Record You Need

Type

TXT

Host / Name

smtp._domainkey

Value
v=DKIM1; k=rsa; p=YOUR_PUBLIC_KEY_FROM_MAILGUN

Mailgun generates a unique DKIM key for each domain. The default selector is smtp or a domain-specific selector.

Step-by-Step Setup

1

Log in to Mailgun

Go to app.mailgun.com and sign in.

2

Select your domain

Go to Sending > Domains and select your verified domain.

3

Copy the DKIM record

Mailgun displays the DKIM TXT record with the selector and public key. Copy both the host name and value.

4

Add the TXT record to DNS

Create a TXT record in your DNS provider with the host and value from Mailgun.

smtp._domainkey.yourdomain.com  TXT  "v=DKIM1; k=rsa; p=MIGfMA0GCSq..."
5

Verify in Mailgun

Click "Verify DNS Settings" in Mailgun. It will check the DKIM record.

DKIM Gotchas

  • Mailgun uses a TXT record for DKIM, not a CNAME. Copy the exact value from your Mailgun dashboard.
  • The DKIM selector varies. Older Mailgun accounts may use smtp._domainkey, while newer accounts may use a domain-specific selector. Always use the selector shown in your dashboard.
  • If using a subdomain (e.g., mg.yourdomain.com), the DKIM record goes under that subdomain: smtp._domainkey.mg.yourdomain.com.

DKIM FAQ

What DKIM selector does Mailgun use?

Mailgun typically uses smtp as the DKIM selector, but newer accounts may use different selectors. Check your Mailgun domain settings for the exact selector.

Is the Mailgun DKIM record a TXT or CNAME?

Mailgun uses a TXT record for DKIM containing the public key directly, unlike some providers that use CNAME records.

Can I rotate DKIM keys in Mailgun?

Yes. You can regenerate DKIM keys in the Mailgun dashboard. After regenerating, update the DNS TXT record with the new public key.

How to Set Up DMARC for Mailgun

Publish a DMARC record to protect your domain when sending transactional email through Mailgun.

Quick Answer — The Record You Need

Type

TXT

Host / Name

_dmarc

Value
v=DMARC1; p=none; rua=mailto:[email protected]; ruf=mailto:[email protected]; fo=1

If using a Mailgun subdomain, add the DMARC record to that subdomain: _dmarc.mg.yourdomain.com.

Step-by-Step Setup

1

Verify SPF and DKIM are configured

Ensure both SPF and DKIM are set up and verified in Mailgun for your sending domain.

2

Send a test email

Use Mailgun's API or SMTP to send a test message and check the Authentication-Results header for SPF and DKIM pass.

3

Create the DMARC record

Add a TXT record at _dmarc.yourdomain.com (or _dmarc.mg.yourdomain.com if using a subdomain).

v=DMARC1; p=none; rua=mailto:[email protected]; ruf=mailto:[email protected]; fo=1
4

Monitor and analyze

Review DMARC reports for 2-4 weeks to ensure Mailgun emails pass alignment.

5

Move to enforcement

Gradually move to p=quarantine and then p=reject.

DMARC Gotchas

  • If Mailgun sends from a subdomain (mg.yourdomain.com), you need a DMARC record on that subdomain. The parent domain DMARC record applies to subdomains only if no subdomain-specific record exists.
  • Mailgun emails align DKIM on the sending domain. Ensure your DKIM record is on the same domain or subdomain you use as the From address.
  • The sp= tag in a parent domain DMARC record controls subdomain policy. If you want different policies for your root domain and Mailgun subdomain, use separate DMARC records.

DMARC FAQ

Do I need DMARC on my Mailgun subdomain?

If your root domain has a DMARC record, it applies to subdomains via the sp= tag (or defaults to the p= policy). You can add a separate DMARC record on the subdomain for a different policy.

Will DMARC affect Mailgun deliverability?

Proper DMARC improves deliverability by signaling to receivers that you actively protect your domain. It only blocks email if authentication fails.

How do I test DMARC alignment with Mailgun?

Send a test email via Mailgun, then inspect the email headers. Look for Authentication-Results showing dmarc=pass and verify the alignment mode (relaxed or strict).

Verify Your Mailgun Setup

Once your SPF, DKIM, and DMARC records are in place, run a full domain scan to confirm everything is configured correctly. DNS changes typically propagate within minutes but can take up to 48 hours.

Setup Guides for Other Providers