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

  1. Go to mailgun.com and sign up
  2. Verify your email address
  3. You'll start with a free tier that includes 5,000 emails/month for the first 3 months
  4. Complete account verification

Get API Key

  1. In your Mailgun dashboard, go to "Sending" → "Domain Settings"
  2. You'll see your API key displayed
  3. Copy the API Key - this is your MAILGUN_API_KEY
  4. Also note your domain (e.g., sandboxxxx.mailgun.org for testing, or your custom domain)
  5. Important: Store the API key securely

Domain Setup

For production, you need to add and verify your sending domain.

Step 1: Add Domain

  1. Go to "Sending" → "Domains" in your Mailgun dashboard
  2. Click "Add New Domain"
  3. Enter your domain (e.g., mg.yourdomain.com - using a subdomain is recommended)
  4. 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

  1. Add the DNS records in your domain's DNS provider
  2. Wait for DNS propagation (can take up to 24 hours, usually faster)
  3. Click "Verify DNS Settings" in Mailgun
  4. 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_KEY comes from "Sending" → "Domain Settings"
  • MAILGUN_DOMAIN is your verified domain or sandbox domain
  • For production, use your custom verified domain
  • For development, you can use the sandbox domain

Testing

  1. Add MAILGUN_API_KEY and MAILGUN_DOMAIN to your .env.local
  2. Restart your dev server: npm run dev
  3. Trigger an email (e.g., sign up, password reset)
  4. Check your email inbox
  5. 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