I'm trying to set up active mailer functionality in my Rails app with Devise and SendGrid so that users can receive a confirmation email when they sign up and can request a forgot password link. When I deploy to Heroku and try and sign up I get an error when I hit 'sign up'. In the Heroku logs the I can see that there is an error as follows:

Completed 500 Internal Server Error in 547ms (ActiveRecord: 8.1ms)
Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password

It's probably something to do with the way I have my config files but I can't put my finger on it. Any help would be massively appreciated! Here are my files.

config/application.yml

production:
  SECRET_KEY_BASE: <%= ENV["SECRET_KEY_BASE"] %>

development:
 GMAIL_USERNAME: [email protected]
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: [email protected]
 SENDGRID_PASSWORD: password

test:
 GMAIL_USERNAME: [email protected]
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: [email protected]
 SENDGRID_PASSWORD: password

production:
 GMAIL_USERNAME: [email protected]
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: [email protected]
 SENDGRID_PASSWORD: password

I'm using the figaro gem to manage my environment variables.

2

There are 2 best solutions below

8
NM Pennypacker On

Your problem appears to be that you're referencing GMAIL_USERNAME and GMAIL_PASSWORD instead of SENDGRID_USERNAME and SENDGRID_PASSWORD, and you're still referencing the Gmail SMTP server and port.

Assuming you're using this gem: https://github.com/stephenb/sendgrid, this should do the trick for your production config file:

config.action_mailer.smtp_settings = {
   address: "smtp.sendgrid.com",
   port: 25,
   domain: "heroku.com",
   authentication: :plain,
   user_name: ENV["SENDGRID_USERNAME"],
   password: ENV["SENDGRID_PASSWORD"],
}

Update: per you comment that you're using the figaro gem, make sure to run the rake task that adds the ENV variables to Heroku: rake figaro:heroku

0
Marcos de Melo On

I use more params too:

ActionMailer::Base.smtp_settings = { . . . enable_starttls_auto: true, openssl_verify_mode: "none" }

:)