SMTP setup not working with GMail's smtp-relay

I can’t get SMTP setup working with Discourse and GMail’s SMTP

I enabled SMTP Relay in Google’s settings, allowed sending from my domain (no IP whitelisting), using SMTP auth + encryption.

Discourse is hosted on forum.example.com

DISCOURSE_SMTP_ADDRESS: smtp-relay.gmail.com
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: noreply@example.com
DISCOURSE_SMTP_PASSWORD: password
DISCOURSE_SMTP_AUTHENTICATION: login
DISCOURSE_SMTP_DOMAIN: mydomain.io
DISCOURSE_NOTIFICATION_EMAIL: noreply@example.com
DISCOURSE_SMTP_ENABLE_START_TLS: true

I had generic error Job exception: end of file reached, when I added NOTIFICATION_EMAIL and SMTP_DOMAIN envs, now I’m seeing

503 5.5.1 bad sequence of commands x20sm63393lfr.126 - gsmtp

when running doctor and trying to send email to myself.

Changed DISCOURSE_SMTP_DOMAIN to match forum.mydomain.io and still have the same error.

How I’m supposed to debug this further other than changing env, rebuilding and hoping that it works?

Thanks in advance.

Instead of rebuilding you can

./launcher destroy app 
./launcher start app 

There are some topics about getting the Google whatever working. It’s possible, but not easy. And it probably changes every month. :wink:

3 Likes

While I belief that @kvsf 's error is either already fixed or some fundamental things within your configuration have been changed I would like to continue this thread by posting my own configuration, my further approach and my seek for help within the same problem-domain.

Basic Configuration

  • Google Workspace Business Starter
  • Configure Gmail-Routing > SMTP-Relay with i) only registered users from within domain, ii) IP-whitelisting with my forum-hosting server-IP and enforcing SMTP-Authentification, iii) enforcing TLS
  • discourse docker installation according to discourse-documentation
  • app.yml config according to
expose:
  - "80:80"   # http
  - "443:443" # https

env:
  DISCOURSE_HOSTNAME: "forum.mydomain.com"
  DISCOURSE_DEVELOPER_EMAILS: 'dev@mydomain.com'

  DISCOURSE_SMTP_ADDRESS: "smtp-relay.gmail.com"
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: "user@mydomain.com"
  DISCOURSE_SMTP_PASSWORD: "mypass"
  DISCOURSE_SMTP_ENABLE_START_TLS: true 
  DISCOURSE_SMTP_AUTHENTICATION: login
  DISCOURSE_SMTP_OPEN_TIMEOUT: 25
  DISCOURSE_SMTP_READ_TIMEOUT: 25
  DISCOURSE_SMTP_DOMAIN: "mydomain.com"
  DISCOURSE_NOTIFICATION_EMAIL: "noreply@mydomain.com"
  LETSENCRYPT_ACCOUNT_EMAIL: dev@mydomain.com

My Tests

When running ./discourse-doctor this configuration leads to the error

Testing sending to ...
SMTP server connection successful.
Sending to artificial.testadress@gmail.com. . .
Sending mail failed.
end of file reached

I then first checked through the different discourse- and email logs
Manually testing the same process from within docker (by connecting using docker exec -it <CONTAINER_ID> bash) via

openssl s_client -starttls smtp -crlf -connect smtp.gmail.com:587

encounters no problems and works fine. So I believe either my parameter-configuration in the app.yml is odd OR discourse internally has some script-miscommunication OR … so many possibilities. ^^
To not always have to ./launcher rebuild app after changing app.yml to test different settings I started to directly edit /var/www/discourse/vendor/bundle/ruby/3.2.0/gems/mail-2.8.1/lib/mail/network/delivery_methods/smtp.rb by changing the existing code to:

class SMTP
  attr_accessor :settings

  DEFAULTS = {
    :address              => 'smtp-relay.gmail.com',
    :port                 => 587,
    :domain               => 'mydomain.com',
    :user_name            => 'user@mydomain.com',
    :password             => 'mypass',
    :authentication       => 'login',
    :enable_starttls      => nil,
    :enable_starttls_auto => true,
    :openssl_verify_mode  => 'peer',
    :ssl                  => nil,
    :tls                  => nil,
    :open_timeout         => 25,
    :read_timeout         => 25
  }

  def initialize(values)
    self.settings = DEFAULTS #.merge(values)
  end

which leads to the same behaviour like above (given the customized app.yml) settings.

Search for help

And now I am stuck. Of course I could play more with the left-over settings (and even could switch back to using SSL-only, even though this is deprecated by discourse), but I’d like to:

  1. learn how to analyze this problem deeper and further
  2. thereby understand what is really going on and what is the problem
  3. fix it and be able to use everything smoothly (+finally get the forum running properly)

Thank you for your help in advance.

push*
Any ideas/suggestions?