Move from Mailgun

PostHog imports your Mailgun bounces, complaints, unsubscribes, mailing lists, and templates across all your domains, in one background import.

Or let your agent do it. Connect the PostHog MCP:

Install the PostHog MCP

npx @posthog/wizard mcp add

Then ask:

“Move our email from Mailgun to PostHog.”

What moves

From MailgunIn PostHog
BouncesSuppression list. Blocks all email.
Spam complaintsSuppression list. Blocks marketing email. Mailgun blocks all email to them.
UnsubscribesOpted out of all marketing email. An unsubscribe from a tag opts out of a new category named after the tag.
Mailing lists, and who left eachMarketing categories and opt-outs
Templates, Handlebars or GoTemplates, converted to Liquid
Domains and allowlistsA list, so you know what to set up

Mailgun has no automations. Routes, webhooks, tracking settings, send history, and stats stay in Mailgun.

Run the import

  1. Create an account API key in Mailgun, with the Support role if you have it. A domain sending key can't read your data, and an Analyst key can't read suppressions. PostHog deletes the key when the import ends.
  2. Go to Messaging > Opt-outs and click Import from another tool.
  3. Pick Mailgun and paste the key. Pick your Mailgun region: US, EU, or both. PostHog only sends your key to the region you pick.
  4. Pick Template versions: Active version only, or Every version, each as its own template. Import every version if your app names a version when it sends. Then pick what to import, and click Start import.
The Mailgun import, with the Mailgun region and Template versions settings

Switch your sending

1. Set up your domain

Set up your domain in PostHog, and publish its DNS records. PostHog needs its own records, even for a domain that already sends through Mailgun. Keep Mailgun's records until nothing sends through it.

2. Move your transactional email

Change your code one email at a time, starting with a low-risk one. You need a project secret API key. These examples use Node.js. Each SDK page has the same send in its language.

If your code writes the HTML, send it as it is. Name the email with a templateKey, so PostHog groups its metrics and logs.

Before, with Mailgun:

JavaScript
import formData from "form-data"
import Mailgun from "mailgun.js"
const mg = new Mailgun(formData).client({ username: "api", key: process.env.MAILGUN_API_KEY })
await mg.messages.create("mg.hedgebox.net", {
from: "Hedgebox <security@hedgebox.net>",
to: ["jane@example.com"],
subject: "Reset your password",
html,
})

After, with PostHog:

JavaScript
import { PostHog } from "posthog-node"
const posthog = new PostHog("<ph_project_token>", {
host: "https://us.i.posthog.com",
secretKey: process.env.POSTHOG_PROJECT_SECRET_API_KEY,
})
const { error } = await posthog.emails.send({
from: "Hedgebox <security@hedgebox.net>",
to: "jane@example.com",
templateKey: "password-reset",
subject: "Reset your password",
html,
distinctId: userId,
})

If you send a Mailgun template, the import copied it into PostHog. The import doesn't make it transactional, because it can't tell which templates your app sends, and a transactional email skips opt-outs and the frequency cap. Open it in the email library, make it transactional, and copy its template ID from its API tab. The values that you sent in t:variables or recipient-variables become its variables.

Before, with Mailgun:

JavaScript
await mg.messages.create("mg.hedgebox.net", {
from: "Hedgebox <hello@hedgebox.net>",
to: ["jane@example.com"],
subject: "Welcome to Hedgebox",
template: "welcome",
"t:variables": JSON.stringify({ name: "Jane", plan: "Pro" }),
})

After, with PostHog:

JavaScript
const { error } = await posthog.emails.send({
to: "jane@example.com",
templateId: "<template_id>",
templateData: { name: "Jane", plan: "Pro" },
distinctId: userId,
})

A send to an address on the suppression list fails with recipient_suppressed. Don't retry it.

Watch for

  • Unsubscribe footer: Mailgun adds one on some domains. PostHog doesn't, so add an unsubscribe link to your marketing emails. The imported domain list says which domains this applies to.

  • Preferences page: people see your imported list and tag names, so rename any that read badly.

  • Images: the import keeps each image's address. An image that Mailgun hosts stops loading once you close your Mailgun account, so copy it into PostHog first.

“Copy the images that Mailgun hosts in our imported templates into PostHog, and point the templates at the copies.”

See switch over safely for the full checklist.

Still have questions?

Was this page useful?