How to set up mail server or SMTP account properly for Discourse?

January 30, 2017 2.5k views
Email DigitalOcean Ubuntu 16.04

I have been trying to set up Discourse for a few days now and I can't find what is causing Discourse not being able to send email to the mail account I provided to be the admin. I have tried setting up Postfix, also using zoho mail (which works for my other websites), mailgun (which seems everything is okay on their website but doesn't work when I sign up for the admin account). Am I forgetting something?

P.S. I am not really experienced about mail servers.

3 Answers

@yanechednezzar

If you simply installed Postfix, but didn't configure it fully or properly, about the only thing it'll be good for out of the box is sending mail locally. External mail will almost always land in the SPAM/Junk bin (i.e. any mail sent to Google, Yahoo, [insert provider]).

When it comes to Postfix, you really need to setup a working mail server and that mail server ideally needs to be on it's own Droplet/VPS/Dedicated, separate from applications, web-based or other.

You also really need to setup SPF, DKIM, DMARC, Black/Grey Lists, Spam and AV, etc. If that sounds like a pain, it's because it really is. I've worked with Linux for over 15 years and I still dread setting up mail servers, even if the script I'm using fully automates the process. It's the constant work required after the setup that's where the pain points are, not really the initial setup.

That said, when it comes to using external services, such as MailGun, Zoho, etc, are you seeing any sort of error? Are you just not receiving the e-mail, is it landing in SPAM/Junk, bouncing, etc?

Even with these services (i.e. MailGun, SendGrid, etc), you still need to run through their process of setting up SPF and DKIM through their services (which is done through their control panels), else mail will still land in the SPAM/Junk bin.

  • Yes I set everything up that is pointed out on their website including SPF and DKIM. For instance MailGun panel says everything is okay. But it doesn't send mail after discourse sign up. For Zoho which I use for my websites, also failing at Discourse sign up.

    I tried setting Discourse up in different ways but I think problem is about mail service.

    I would be happy to find the mails in SPAM/Junk bin, it doesn't send at all.

@yanechednezzar

If there's a connection attempt, there should be some sort of a log stating whether that attempt was successful or whether it failed (at the mail provider -- either through their CP or otherwise).

Even if there's not, there should be some sort of log, somewhere, that details what is causing mail not to go through, or causing it to fail to send.

Have you checked your error logs? You can check /var/log and run a command such as:

tail -20 /var/log/LOGFILE

Where LOGFILE is the name of the log file you wish to get output from. The above command will allow you to echo out the last 20 lines of the log. You can, of course, swap 20 out with 50, 100, etc, though normally and in most cases, the last 20 lines are enough.

Beyond that, you may wish to check and see if there's an error log being stored for Discourse and also scan it to see if there's something being logged. If it fails and there's no log anywhere, that's a major issue as something beyond mail service is probably an issue.

It could be something small, or it could be an actual issues which you'd need to report to the devs.

With that being said, how did you setup Discourse? If you'll let me know how, I'll run it on a tester Droplet of mine and see if I can replicate the issue.

  • Yes I should check the log files now. You can test it from One-Click apps. (Discourse on 14.04) I followed this

    DigitalOcean's Discourse one-click application image provides an easy way to get started with a Discourse forum. This tutorial will walk you through creating and setting up a new Discourse Droplet. Based on Ubuntu 16.04, the Discourse one-click...
    • @yanechednezzar

      Just wanted to drop and a note and say that I'l working on it :-). The image provided by the one-click installer didn't work for me as far as setup goes, so I'm having to run the builder script to rebuild the container.

      The startup kept throwing an error about lines in the yml file not being valid (specifically the line in the configuration where I define the SMTP password). That's what I've found thus far, though I'm still waiting on the rebuild to finish up.

      I had to remove the comments from the yml file before I could even start the rebuild, so I'm not sure if that's related or not.

    • @yanechednezzar

      First build didn't send e-mail using my Google G Suite configuration which is valid and has been working since I set it up, so falling back to the Discourse FAQ on troubleshooting mail issues.

      Essentially, cd in to /var/discourse, open containers/app.yml, and uncomment:

      #- exec: rails r "SiteSetting.notification_email='noreply@example.com'"
      

      ... and set the e-mail address above to a valid e-mail. That way this runs on the rebuild.

      Will report back on whether this works or not. If not, I'm honestly not sure what's wrong as none of my ports are blocked (everything is wide open), thus in & external communication shouldn't be an issue.

    • @yanechednezzar

      Ok, working now :-).

      So what it boils down to is this one file:

      /var/discourse/containers/app.yml
      

      In the above file there's a few lines that ultimately decide whether or not e-mail is going to work. Those lines are:

        DISCOURSE_SMTP_ADDRESS:
        DISCOURSE_SMTP_PORT:
        DISCOURSE_SMTP_USER_NAME:
        DISCOURSE_SMTP_PASSWORD:
      

      That said, I had to strip out all the comments from the app.yml file. I kept getting errors with them, so I just deleted all lines with a # in front.

      For Google's G Suite (formerly Google Apps), I had to set the SMTP address to:

      smtp-relay.gmail.com
      

      Set the Port to 587 and leave the username and password blank. The reason for this is because of the relay -- which has to be setup inside the Google G Suit control panel.

      For MailGun and similar, unless otherwise instructed, you should be able to simply set the address they provide, the port (which will most likely be 587), and then the username and password.

      Once that's set, I had to rebuild the container, which I did by:

      cd /var/discourse
      

      ... then

      ./launcher rebuild app
      

      Once rebuilt, e-mail instantly began working.

      • Thank you very much! I'm not sure about whether I uncommented the lines right or not. Because I couldn't even enter the website when I did that. Error was something "enUS-UTF-8" something something. But uncommenting "#- exec: rails r "SiteSetting.notificationemail='noreply@example.com'"" made everything work. So now at least I passed through admin sign up. If anything else shows up I'll try uncommenting everything again.

        Thank you very much again.

What did you end up using? Did you setup Zoho for the mails? I'm having a similar issue, and have already set up the DKIM and SPF for the domain.

The discourse forum is setup in one of my sub domains in a different droplet. Do I have to set up DKIM and SPF for the sub domain as well? I tried sending test emails using Discourse but it doesn't work.

The log message states "Sent mail to something@gmail.com (1329.1ms)
Completed 422 Unprocessable Entity in 1451m"

  • In the end I used MailGun. " But uncommenting "#- exec: rails r "SiteSetting.notificationemail='noreply@example.com'"" made everything work. " I said.

    So I didn't make inbox. It's just using mailgun to send the email verification upon sign up.

Have another answer? Share your knowledge.