Mailgun
Last updated December 18, 2025
Overview
Mailgun is an email service provider that specializes in transactional emails and inbound email processing. It's great for high-volume sending and provides detailed analytics and tracking.
Account Setup
- Go to mailgun.com and sign up
- Verify your email address
- You'll start with a free tier that includes 5,000 emails/month for the first 3 months
- Complete account verification
Get API Key
- In your Mailgun dashboard, go to "Sending" → "Domain Settings"
- You'll see your API key displayed
- Copy the API Key - this is your
MAILGUN_API_KEY - Also note your domain (e.g.,
sandboxxxx.mailgun.orgfor testing, or your custom domain) - Important: Store the API key securely
Domain Setup
For production, you need to add and verify your sending domain.
Step 1: Add Domain
- Go to "Sending" → "Domains" in your Mailgun dashboard
- Click "Add New Domain"
- Enter your domain (e.g.,
mg.yourdomain.com- using a subdomain is recommended) - Click "Add Domain"
Step 2: Add DNS Records Mailgun will show you the DNS records to add:
- TXT Record (SPF):
v=spf1 include:mailgun.org ~all - CNAME Records (DKIM): Multiple CNAME records (Mailgun provides exact values)
- TXT Record (DMARC):
v=DMARC1; p=none; rua=mailto:dmarc@yourdomain.com - MX Records (for inbound): Optional, only if you want to receive emails
Step 3: Verify Domain
- Add the DNS records in your domain's DNS provider
- Wait for DNS propagation (can take up to 24 hours, usually faster)
- Click "Verify DNS Settings" in Mailgun
- Once verified, you can send from addresses like
noreply@mg.yourdomain.com
For Testing:
You can use Mailgun's sandbox domain (e.g., sandboxxxx.mailgun.org) without DNS setup, but emails may have delivery limitations.
Environment Variables
Add these variables to your .env.local file:
# Mailgun MAILGUN_API_KEY="your-mailgun-api-key" MAILGUN_DOMAIN="mg.yourdomain.com" # or sandboxxxx.mailgun.org for testing
Important:
MAILGUN_API_KEYcomes from "Sending" → "Domain Settings"MAILGUN_DOMAINis your verified domain or sandbox domain- For production, use your custom verified domain
- For development, you can use the sandbox domain
Testing
- Add
MAILGUN_API_KEYandMAILGUN_DOMAINto your.env.local - Restart your dev server:
npm run dev - Trigger an email (e.g., sign up, password reset)
- Check your email inbox
- In Mailgun dashboard → "Sending" → "Logs", you can see email delivery status
Test Email Script:
import formData from 'form-data'; import Mailgun from 'mailgun.js'; const mailgun = new Mailgun(formData); const mg = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY!, }); await mg.messages.create(process.env.MAILGUN_DOMAIN!, { from: `noreply@${process.env.MAILGUN_DOMAIN}`, to: ['your-email@example.com'], subject: 'Hello', text: 'Testing Mailgun!', });
Troubleshooting:
- If emails don't arrive, check Mailgun logs for error messages
- Verify API key and domain are correct
- For production, ensure domain is verified
- Check spam folder, especially when using sandbox domain
- Review Mailgun's sending limits and quotas