0

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: scott.ian.munro@gmail.com
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: app115625999@heroku.com
 SENDGRID_PASSWORD: password

test:
 GMAIL_USERNAME: scott.ian.munro@gmail.com
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: app115625999@heroku.com
 SENDGRID_PASSWORD: password

production:
 GMAIL_USERNAME: scott.ian.munro@gmail.com
 GMAIL_PASSWORD: password
 SENDGRID_USERNAME: app115625999@heroku.com
 SENDGRID_PASSWORD: password

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

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
Steve
  • 418
  • 1
  • 4
  • 16

2 Answers2

1

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

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
  • Sorry, I had actually posted the wrong code for my production file (have updated now). I actually already have `SENDGRID_USERNAME` and `SENDGRID_PASSWORD` in there and still get the error. – Steve Feb 13 '19 at 21:04
  • @ NM Pennypacker Also, when I try these in the console I get `nil` p ENV[‘SECRET_DEV_KEY’] p ENV[‘SECRET_KEY_TEST’] p ENV[‘SECRET_KEY_BASE’] – Steve Feb 14 '19 at 00:24
  • You may have to add the `ENV` variables somewhere in the Heroku admin UI. If they're coming up `nil` in the console, they're not being loaded. How are you loading the data from application.yaml into the app? – NM Pennypacker Feb 14 '19 at 13:01
  • I'm using the figaro gem, basically following [this](https://medium.com/@cheney.shreve/using-devise-with-gmail-sendgrid-rails-and-heroku-tips-to-save-time-debugging-626546118358) tutorial – Steve Feb 14 '19 at 16:14
  • See the Update: Looks like your ENV variables aren't making it to Heroku – NM Pennypacker Feb 14 '19 at 16:17
  • it doesn't recognise that command: rake aborted! Don't know how to build task 'figaro:heroku' (see --tasks) /home/scott_imunro/.rbenv/versions/2.4.2/bin/ruby_executable_hooks:24:in `eval' /home/scott_imunro/.rbenv/versions/2.4.2/bin/ruby_executable_hooks:24:in `
    '
    – Steve Feb 14 '19 at 16:36
  • I don't think it's a heroku issue as i'm getting errors in development too: `Puma caught this error: Missing `secret_key_base` for 'development' environment, set this value in `config/secrets.yml` (RuntimeError)` – Steve Feb 14 '19 at 16:37
  • Then you probably have to install the heroku command line tools. Either way, you'll have to be able to run that command – NM Pennypacker Feb 14 '19 at 16:47
0

I use more params too:

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

:)