Mailgun for Email from your application

I've been using Mailgun for a while now to send transactional emails, I decided to turn it into a blog post just to walk through the settings.

Mailgun is a brilliant tool for email. It's very affordable and if your an indie maker like myself it makes great sense.

Mailgun is very flexible you can send via their API or with SMTP.

What we will look at in this post

  • DNS Settings
  • Webhooks

I have a post where I dig into DNS

DNS Settings

The first part once you have Mailgun set up you need to configure your DNS. This lets you send with your domain name. Mailgun will show you what DNS records you need to set.

TXT Records

These are DNS entries that are used for verification and also anti spam. Sometimes they will be called SPF / DKIM records.

These will verify the email came from you.

MX Records

MX Records are typically used only when receiving emails. If you want Mailgun to receive emails for you, you need to set these other wise no you don't set them.

CNAME

There is a CNAME record needed to be setup. This allows mailgun to track the email, delivery and open.

This will usually look like email.yourdomain.com (but you can change it from email to something else if that's already used)

This means that any links in the emails are branded to your domain name and not mailgun. But it can still track all the clicks.

Note: If you are using cloudflare the cname domain shouldn't be proxied through cloudflares network, this causes an issue.

That's it you are now able to send emails through Mailgun.

Webhooks

Mailgun has a set of webhooks for different events. Email Delivered, Email Failed, etc.

I use these webhooks to track events on emails, for example. A user signs up to Contentr I send the welcome email but I take the ID from Mailgun and store it. If the email fails I catch that in a webhook.

This lets me know the email address was invalid and I can then lock the email account. This is all done automatically with out me checking anything.