How to solve SMTPAuthentication error / Ruby on Rails app, Devise, SendGrid hosted on Heroku

379 Views Asked by At

Good day guys, I am working on a rails application that uses devise to send confirmation emails to registered users. The app properly responds on the development environment and the mails can be caught by mailcatcher. After pushing on heroku my logs reveal a SMTPAuthentication error which leads to a 500 server error each time a user is registered. The logs also reveals that the message gets sent before the error pops up.

2020-02-23T16:04:03.373626+00:00 app[web.1]: 
2020-02-23T16:04:03.373627+00:00 app[web.1]: <p>You can confirm your account email through the link below:</p>
2020-02-23T16:04:03.373627+00:00 app[web.1]: 
2020-02-23T16:04:03.373628+00:00 app[web.1]: <p><a href="https://own-your-life-campaign.herokuapp.com//users/confirmation?confirmation_token=dsbs-bckkBo-qTcuoNLa">Confirm my account</a></p>
2020-02-23T16:04:03.373629+00:00 app[web.1]: 
2020-02-23T16:04:03.373629+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b] Completed 500 Internal Server Error in 152ms (ActiveRecord: 5.4ms)
2020-02-23T16:04:03.373630+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b]
2020-02-23T16:04:03.373630+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b] Net::SMTPAuthenticationError (530-5.7.0 Authentication Required. Learn more at
2020-02-23T16:04:03.373631+00:00 app[web.1]: ):

Here is my action mailler SMTP configuration on my environment/production.rb file.

# ActionMailer Config
  config.action_mailer.default_url_options = { 
    :host => 'https://own-your-life-campaign.herokuapp.com/'
  }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true

  ActionMailer::Base.smtp_settings = {
   :user_name => ENV['SENDGRID_USERNAME'],
   :password => ENV['SENDGRID_PASSWORD'],
   :domain => 'own-your-life-campaign.herokuapp.com',
   :address => 'smtp.sendgrid.net',
   :port => 587,
   :authentication => :plain,
   :enable_starttls_auto => true
  }

I also added SendGrid addons on heroku and generated a user name and a password, here is a sample of my environment variables on heroku. am omitting my secrete key base and SENDGRID_API key for security reasons.

SECRET_KEY_BASE:          ------------------------------------------------------------------
SENDGRID_API_KEY:         -------------------------------------------------------------------
SENDGRID_PASSWORD:        -------------------------------------
SENDGRID_USERNAME:        --------------------------------------

After creating the SendGrid api key, I verified it on their platform and it gave me a 200 status confirming which is ok. I also changed Api key settings to full access. I don't know if there are further settings I need to do on their platform in other for my post request to properly authenticate from my rails app or It's the fact that am using a Free plan?.

0

There are 0 best solutions below