Using MailGun Europe with Rails’s ActionMailer.

2019-12-03

You’d think this isn’t worth a blogpost, since Mailgun has a dedicated mailgun-ruby gem for sending emails and it integrates well with Rails’s ActionMailer. However when setting this up last night a couple of errors were thrown my way that took some time to resolve.

Mailgun::ParseError (765: unexpected token at 'Mailgun Magnificent API')

First, when setting up a new Mailgun domain, it will ask you what language you’re using and shows an API key and a URL to use for sending emails.

In Rails the ActionMailer config in config/environments/production.rb looks like this:

  config.action_mailer.mailgun_settings = {
    :api_key => <your_api_key>,
    :domain => <your_domain>,
  }

If you enter the full url from the Mailgun page in the domain field, Mailgun will return a Mailgun::ParseError (765: unexpected token at 'Mailgun Magnificent API') error. You have to change the domain to your sending domain (e.g. mg.matsimitsu.com).

Mailgun API response 404 (NOT FOUND)

Second if you selected Europe as the region for your new domain, and you fixed the issue above, you’ll get a new error along the lines of Mailgun API response 404 (NOT FOUND).

What’s happening is that the API can’t find your credentials/domain, since it’s hosted in another region.

Now the Gem documentation doesn’t mention this (yet, multiple pulls have been made, but somehow none have been merged yet).

You have to specify a 3rd parameter to the ActionMailer config, api_url that points to the Europe region API.

Your config/environments/production.rb should contain something like:

  config.action_mailer.delivery_method = :mailgun
  config.action_mailer.mailgun_settings = {
    :api_key => <your_api_key>,
    :domain => <your_domain>, # e.g. mg.matsimitsu.com
    :api_host => "api.eu.mailgun.net"
  }

And of course we use ENV vars in production:

  config.action_mailer.delivery_method = :mailgun
  config.action_mailer.mailgun_settings = {
    :api_key => ENV["MAILGUN_API_KEY"],
    :domain => ENV["MAILGUN_DOMAIN"],
    :api_host => "api.eu.mailgun.net"
  }

With these two fixes applied and deployed, emails were flowing again \o/.

More from the blog

- Resize images from s3 with AWS Lambda and Rust

My site contains a lot of images and resizing them for different devices (mobile phone, tabled, desktop etc.) takes a lot of time and (upload) bandwidth. This is especially annoying on Hotel Wi-Fi in a far-away country. With the help of AWS Lambda and Rust I made this into a smooth process.

- Taming our MongoDB database size.

Not so long ago we noticed that our MongoDB servers were running out of disk space at an alarming rate. Because we host our database on SSD enabled servers at DigitalOcean, scaling up could cost us a lot of money.