You've successfully subscribed to Mailing List Hackers
Great! Next, complete checkout for full access to Mailing List Hackers
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

How to set up Mailgun for your Ghost blog

Kristian Freeman
Kristian Freeman

In this (hopefully short!) tutorial, I'll show you how to set up Mailgun on your Ghost blog. A number of Ghost features, including inviting authors, resetting passwords, and more recently, handling login for Ghost's Members functionality -- as detailed in our "Setting up a membership program for your Ghost blog" guide -- require Mailgun for sending emails.

Mailgun is a free service used by developers to send emails. Sending emails that don't get detected as spam by users is a pretty hard problem, and if you're writing blog posts or newsletters, it's the kind of thing that you shouldn't try and do yourself. Luckily, setting up Mailgun for your blog is mostly painless, requiring just a little bit of configuration internally, and some work over on Mailgun's dashboard.

Requirements

  • A configured Ghost blog
  • Access to SSH or the filesystem of your Ghost blog (in this tutorial, we'll use DigitalOcean – referral link)
  • A custom domain you own (if you don't own one, I recommend buying with Hover (referral link)

Setting up your Mailgun account

If you haven't already, you'll need to create a Mailgun account. You can sign up for an account here, and once you've verified your email address, you'll need to set up your first Mailgun domain.

With your custom domain (e.g. mailinglisthackers.com) set up, follow Mailgun's DNS instructions to set up a subdomain of your site for sending emails with Mailgun. Most commonly, this is done at the mg subdomain. For detailed instructions on this, check out Mailgun's instructions page on "How to start sending email".

When you've validated DNS records for your domain, you should receive an email address and password that will allow you to send emails through Mailgun. If you don't receive that, go to "Domain Settings", and "SMTP credentials", reset the SMTP Credentials password, and make sure to save it somewhere for later in the guide.

Once you've set up a Mailgun domain, you should have an email and password provided by Mailgun that matches your chosen domain. Keep this tab open, as we'll use it for configuring Ghost.

Adding Mailgun support to Ghost

Just a heads-up, this part of the tutorial will likely be the trickiest section for non-developers. If you run into problems, consider joining the Mailing List Hackers Discord server, where you can chat with other members of the community and get help with this set up process.

To configure Mailgun support for your Ghost blog, you'll need access to your blog's configuration file: this is a JSON file in the filesystem of the server hosting your Ghost blog, where you can specify information about your blog that isn't available via the Ghost administrator panel. As mentioned previously, this guide assumes the use of DigitalOcean, our favorite platform for deploying Ghost blogs (this site is hosted on DO!). In your DigitalOcean dashboard, find your Ghost blog's "droplet" (the server running your blog) and select "Access Console" in the "..." dropdown at the right of your droplet name. This will open a new window that emulates SSH, or "Secure Shell", and allows you to interface with your server's filesystem.

Now that we're in your blog's filesystem, before we make any changes, we should backup the configuration file for your blog, in case something goes wrong and we want to revert our changes. Write the following command into your SSH window and press enter:

$ cp /var/www/ghost/config.production.json /var/www/ghost/config.production.json.backup

This will make a copy of your configuration file -- later in the guide, I'll show you how to revert your changes if something goes wrong!

To fill in your Mailgun information, you'll need to open up your blog's JSON configuration file using a text editor. Enter the following command into the SSH window, and then press enter:

$ nano /var/www/ghost/config.production.json

This will open the configuration file in nano, a terminal-based text editor. Scroll down using the arrow keys to the section "mail", which tells Ghost how to send emails. Using the backspace key, remove the mail section, and replace it with the following, filling in the email and password fields for your Mailgun domain:

{
  "mail": {
    "transport": "SMTP",
    "options": {
      "service": "Mailgun",
      "auth": {
        "user": "YOUR_MAILGUN_EMAIL",
        "pass": "YOUR_MAILGUN_PASSWORD"
      }
    }
  }
}

With your Mailgun configuration set up, it's time to restart Ghost. Run the following command to log in as the Ghost user in your server, which will allow you to administrate your Ghost blog:

$ sudo -i -u ghost-mgr

Now that you're logged in as the ghost-mgr user, you can navigate to your Ghost blog directory and restart your blog for the new Mailgun changes to be applied:

$ cd /var/www/ghost
$ ghost restart

If everything goes as expected, you should be able to navigate to your blog in browser and see everything looking like you'd expect. You can test your email functionality by going to the "Authors" section of your Ghost admin panel, and inviting a new user (providing an email of your choice) -- if you (or another author) receive an email in their inbox, your Mailgun set up is working!

If something goes wrong and your blog returns an error code in browser, don't fret! We made a backup for this exact reason. To restore the backup, we can replace the current configuration file with our backup file:

$ sudo cp /var/www/ghost/config.production.json.backup /var/www/ghost/config.production.json

If you're unable to run the above command using sudo, you may need to log back out of the ghost-mgr command into the default root user, by running exit. Once you're logged back in as root (which you can check by running echo $USER), run the above command, removing the sudo at the beginning.

Once you've copied the backup file, you can restart the Ghost server again to return to your old configuration file:

$ ghost restart

If you're unable to get your changes to stick, consider joining our Discord server -- there's a community of people that can help with your configuration.

Join our chat community
Our chat server is where Mailing List Hackers hang out and talk shop about what they’re working on, help each other with mailing lists, and share our wins as we grow our lists! Sign up as a member to get your invite.

With Mailgun set up for your Ghost blog, you can now send emails to new authors, new members, and whatever else Ghost needs email for. While you're here, consider checking out some of our other tutorials on Ghost -- for instance, how to set up a paid membership program. Happy blogging!

GhostGuidesMailgun

Kristian Freeman

I'm a programmer and writer — I also founded Mailing List Hackers! I love to teach, write, and make stuff online.