Configuring Mailgun via Ghost-CLI results in HTTP error 502

If you’re looking for some help, it’s important to provide as much context as possible so that people are able to assist you. Try to always mention:

  • What’s your URL? https://www.ewesparky.com/

  • What version of Ghost are you using? 3.3.0

  • What configuration? DigitalOcean pre-configured Ghost droplet

  • What browser? Firefox 71 on Windows 10 1809

  • What errors or information do you see in the console? See below

  • What steps could someone else take to reproduce the issue you’re having?

    1. Install DigitalOcean droplet
    2. Configure Mailgun SMTP via Ghost-CLI
    3. Restart Ghost instance
    4. Check if HTTP error 500/502 generated

Hi there,

I had previously tried to configure mail using the admin site, but I wanted to use a different domain to my site (email.mydomain… rather than www.mydomain… as shown in Ghost admin). The API would return an error that it failed to send, so I deleted those settings and tried it via Ghost-CLI instead.

I tried to configure mail using Mailgun via SMTP with ghost config followed by ghost restart. No error appeared in the terminal, but my site started to return a HTTP error 502.

I checked the https...production.error.log logfile, which shows the following:

    {
        "name": "Log",
        "hostname": "da-ghost",
        "pid": 26088,
        "level": 50,
        "err": {
            "id": "c9abe4c0-36a9-11ea-9162-9b5350eb082d",
            "domain": "https://www.ewesparky.com",
            "code": null,
            "name": "EmailError",
            "statusCode": 500,
            "level": "normal",
            "message": "Failed to send email. Reason: Can't send mail - all recipients were rejected.",
            "help": "\"Please see https://ghost.org/docs/concepts/config/#mail for instructions on configuring email.\"",
        "stack": "EmailError: Failed to send email. Reason: Can't send mail - all recipients were rejected.\n    at EmailError.GhostError (/var/www/ghost/versions/3.2.0/core/server/lib/common/errors.js:10:26)\n    at new EmailError (/var/www/ghost/versions/3.2.0/core/server/lib/common/errors.js:34:20)\n    at createMailError (/var/www/ghost/versions/3.2.0/core/server/services/mail/GhostMailer.js:50:12)\n    at EventEmitter.<anonymous> (/var/www/ghost/versions/3.2.0/core/server/services/mail/GhostMailer.js:116:28)\n    at Object.onceWrapper (events.js:286:20)\n    at EventEmitter.emit (events.js:198:13)\n    at EventEmitter.emit (domain.js:448:20)\n    at DirectMailer.<anonymous> (/var/www/ghost/versions/3.2.0/node_modules/directmail/lib/mailer.js:185:42)\n    at SMTPClient.<anonymous> (/var/www/ghost/versions/3.2.0/node_modules/directmail/lib/mailer.js:277:9)\n    at Object.onceWrapper (events.js:286:20)\n    at SMTPClient.emit (events.js:198:13)\n    at SMTPClient.EventEmitter.emit (domain.js:448:20)\n    at SMTPClient._onError (/var/www/ghost/versions/3.2.0/node_modules/simplesmtp/lib/client.js:373:10)\n    at SMTPClient._actionRCPT (/var/www/ghost/versions/3.2.0/node_modules/simplesmtp/lib/client.js:1054:18)\n    at SMTPClient._onData (/var/www/ghost/versions/3.2.0/node_modules/simplesmtp/lib/client.js:354:29)\n    at Socket.emit (events.js:198:13)\n    at Socket.EventEmitter.emit (domain.js:448:20)\n    at addChunk (_stream_readable.js:287:12)\n    at readableAddChunk (_stream_readable.js:268:11)\n    at Socket.Readable.push (_stream_readable.js:223:10)\n    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)"
        },
        "msg": "Failed to send email. Reason: Can't send mail - all recipients were rejected.",
        "time": "2020-01-14T08:42:24.914Z",
        "v": 0
    }

After getting the above error, I tried ghost doctor:

✔ Checking system Node.js version
✔ Checking logged in user
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
✔ Checking operating system compatibility
✔ Checking for a MySQL installation
+ sudo systemctl is-active ghost_www-ewesparky-com
Instance is currently running
ℹ Validating config [skipped]
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
✔ Checking binary dependencies

I rebooted, but the error remained. I removed the mail settings from config.production.json and ran ghost restart. The site came back online immediately.

Any ideas why configuring mail results in an error 502?

Thanks for your help.

OK, I ended up nuking the mail options in config.production.json and restarting Ghost. That allowed the site to come back online.

A couple of things that confused me…

This is the mail config in the Docs:

"mail": {
    "transport": "SMTP",
    "options": {
        "service": "Mailgun",
        "host": "smtp.eu.mailgun.org",
        "port": 465,
        "secureConnection": true,
        "auth": {
            "user": "postmaster@example.mailgun.org",
            "pass": "1234567890"
        }
    }
}

It seems like ghost config can be run with either ghost config set key value or with ghost config --key value. Are both options valid?

When I use ghost config set key value, my config file looks like:

{
  "url": "https://www.example.com",
  "server": {
    "port": 2368,
    "host": "127.0.0.1"
  },
  "database": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "ghost",
      "password": "my database password",
      "database": "ghost_production"
    }
  },
  "mail": "SMTP",
  "logging": {
    "transports": [
      "file",
      "stdout"
    ]
  },
  "process": "systemd",
  "paths": {
    "contentPath": "/var/www/ghost/content"
  },
  "mailservice": "Mailgun",
  "mailuser": "whatever@email.example.com",
  "mailpass": "my secret mail password",
  "mailhost": "smtp.eu.mailgun.org",
  "mailport": 587
}

It looks like one of two things were tripping me up and causing the HTTP errors:

  • The nginx port in the Ghost config would change occasionally from 2368 to 2369, causing a mismatch (I didn’t manually change this to 2369, so I’m not sure how/why this was happening).
  • If I manually edited the config file with nano and used the example above from the Docs, Ghost would think the config was invalid.

I’m confused as to which json format is correct.

I left it with the settings/format that ghost config set key value created, however, when I try to test the mail settings through the admin site, I get the following error. The test mail never arrives at Mailgun. (Mailgun is satisfied the DNS settings, etc are correct, and I can send mail through Mailgun on a subdomain Discourse site):

[2020-01-15 18:47:35] ERROR "POST /ghost/api/v3/admin/mail/test/" 500 1638ms

NAME: EmailError
MESSAGE: Failed to send email. Reason: Can't send mail - all recipients were rejected.

level: normal

"Please see https://ghost.org/docs/concepts/config/#mail for instructions on configuring email."
EmailError: Failed to send email. Reason: Can't send mail - all recipients were rejected.
    at EmailError.GhostError (/var/www/ghost/versions/3.3.0/core/server/lib/common/errors.js:10:26)
    at new EmailError (/var/www/ghost/versions/3.3.0/core/server/lib/common/errors.js:34:20)
    at createMailError (/var/www/ghost/versions/3.3.0/core/server/services/mail/GhostMailer.js:50:12)
    at EventEmitter.<anonymous> (/var/www/ghost/versions/3.3.0/core/server/services/mail/GhostMailer.js:116:28)
    at Object.onceWrapper (events.js:286:20)
    at EventEmitter.emit (events.js:198:13)
    at EventEmitter.emit (domain.js:448:20)
    at DirectMailer.<anonymous> (/var/www/ghost/versions/3.3.0/node_modules/directmail/lib/mailer.js:185:42)
    at SMTPClient.<anonymous> (/var/www/ghost/versions/3.3.0/node_modules/directmail/lib/mailer.js:277:9)
    at Object.onceWrapper (events.js:286:20)
    at SMTPClient.emit (events.js:198:13)
    at SMTPClient.EventEmitter.emit (domain.js:448:20)
    at SMTPClient._onError (/var/www/ghost/versions/3.3.0/node_modules/simplesmtp/lib/client.js:373:10)
    at SMTPClient._actionRCPT (/var/www/ghost/versions/3.3.0/node_modules/simplesmtp/lib/client.js:1054:18)
    at SMTPClient._onData (/var/www/ghost/versions/3.3.0/node_modules/simplesmtp/lib/client.js:354:29)
    at Socket.emit (events.js:198:13)
    at Socket.EventEmitter.emit (domain.js:448:20)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

From that error, it seems like Ghost is trying to use Direct instead of SMTP.

If I go back and manually edit config.production.json so that it’s now:

{
  "url": "https://www.example.com",
  "server": {
    "port": 2368,
    "host": "127.0.0.1"
  },
  "database": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "ghost",
      "password": "my database password",
      "database": "ghost_production"
    }
  },
  "mail": {
    "transport": "SMTP",
    "from": "'My Email User' <whatever@example.com>"
  },
  "logging": {
    "transports": [
      "file",
      "stdout"
    ]
  },
  "process": "systemd",
  "paths": {
    "contentPath": "/var/www/ghost/content"
  },
  "mailservice": "Mailgun",
  "mailuser": "whatever@email.example.com",
  "mailpass": "my secret mail password",
  "mailhost": "smtp.eu.mailgun.org",
  "mailport": 587
}

and then retest the email through the admin site, I get a message saying “Check your email for the test message.” I also tried logging out and then sending a login link for my test member account. Neither email arrives at Mailgun. I copied the Mailgun email user password straight out of their dashboard and into the ghost config set mailpass ... command, so I don’t think it’s that!

Any ideas what might be going on here?

Thanks for your help!