The default way to add Mailgun credentials for sending newsletters with Ghost is in the Newsletter settings, by pasting in your API key and domain into the form.

But in some instances this is cumbersome, or simply not possible. For example, you may be setting up a self-hosted site for a client, which means you can't log into the Ghost admin to set these variables.

Luckily there's a second way to set the Mailgun credentials: in the Ghost config file (typically config.production.json in the root folder of a Ghost installation).

You can either paste in the data directly into this file or use the Ghost CLI client to add them.

Set Mailgun credentials in the config file

Paste the following bulkEmail object into your config file. Make sure to add your own API key, domain and check the base URL matches your account:

{
  "url": "https://yoursite.com",
  ...
  "bulkEmail": {
    "mailgun": {
      "apiKey": "MAILGUN_API_KEY",
      "domain": "MAILGUN_DOMAIN",
      "baseUrl": "https://api.mailgun.net/v3"
    }
  }
}

Remember to run ghost restart after editing the config file, to apply the changes.

Set Mailgun credentials using the command line

Alternatively, you can run these commands to add the same info to your config file:

ghost config bulkEmail.mailgun.apiKey MAILGUN_API_KEY
ghost config bulkEmail.mailgun.domain MAILGUN_DOMAIN
ghost config bulkEmail.mailgun.baseUrl https://api.mailgun.net/v3

As with above, remember to run ghost restart afterwards to apply the changes.

Bonus: Set a "tag"

If you are using the same Mailgun account across many sites, you will not be able to filter logs or analytics across your different sites... unless you specify a "tag" for each email sent.

In Ghost you can specify a tag value to attach a tag to your newsletter emails, which will then let you drill down by site in your Mailgun reporting interface.

In the config file, add a "tag" value:

"bulkEmail": {
  "mailgun": {
    "apiKey": "MAILGUN_API_KEY",
    "domain": "MAILGUN_DOMAIN",
    "baseUrl": "https://api.mailgun.net/v3",
    "tag": "SITE_NAME"
  }
}

In the command line, run this extra command:

ghost config bulkEmail.mailgun.tag SITE_NAME

Thanks for reading!

Dan Rowden   Dan Rowden

I am the founder of Codelet. I have five years experience publishing and developing for Ghost, on over 100 sites. Codelet publishes Ghost themes, blog posts and offers expert Ghost support.