Setting Up Mailgun with Rails 6 Running on Dokku

After going through documentation and trying out several options to set up mailing services on Rails apps, I settled on Mailgun. While I don't need to send a ton of email, at least yet, the pricing wasn't my first priority provided the lowest tier was either free or nominally cheap.

In the case of Mailgun, they currently give you the first 3 months free up to 5000 emails. After that, with their flex plan tier, it costs 80 cents per 1000 emails. While there may be cheaper options, such as Sendgrid or even just setting up Gmail's servers for a limited amount of free emails, I found Mailgun to be the easiest and most reliable. I also loved Mailgun's interface and reporting.

So, here's how to set up Mailgun using their API key, which is recommended. You could opt for SMTP forwarder, but the API method is faster and is better suited for long term usage. There are a few extra steps with setting up your own domain for full API key usage, but the documentation and instruction provided by Mailgun to edit your DNS records is phenomenal and easy to follow.

So, let's do this!

1. Sign up for Mailgun Service.

Yes, you will need to provide a credit card. Like mentioned before on the flex plan, even after the trial, there is a nominal cost of $0.80 per 1000 emails. If you need more emails, you can choose a plan and the cost per thousand gets appreciably lower.

2. Go to the Domains section on the left hand menu, then look for the "Add New Domain" on the right side.

This is where you will start the process of adding your domain. They give really clear documentation on how to handle this. While you could just use your basic domain such as "", it is recommended you create a subdomain such as "" Once you choose what your subdomain is, Mailgun provides explicit instructions for what you need to modify in your DNS records.

3. After the domain is set up, you need the mailgun-ruby gem added to your Gemfile.

At the time of this writing, this is the current gem:

gem 'mailgun-ruby', '~> 1.2' 

You can check Rubygems to see if there is an update.

Add the gem to your Gemfile and then run:

bundle install

4. Edit the production.rb file in /config/enviroments.

Look for the line, config.action_mailer.perform_deliveries = true. You will need to add the following lines below (technically, you could add them anywhere in the file but I like to keep similar attributes together), to look like this:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
api_key: ENV['MAILGUN_API_KEY'],
domain: ENV['DOMAIN_NAME']

5. Log onto your Dokku server via SSH and define the variables for your API key and the domain.

$ dokku config:set <your_app> MAILGUN_API_KEY=<your_api_key>
$ dokku config:set <your_app> DOMAIN_NAME=<your_domain_you_set> #e.g

Just a reminder if you have trouble locating your private API key in Mailgun, click on the Dashboard and look on the right hand side under you account settings. You will see API Keys at the top of the menu. Just click that to reveal your private API key.

6. The last step is to be sure and push the new codebase onto your server.

$ git push dokku master

That's it... you should have Mailgun working and ready to go.


Please log in to leave a reply.