Ruby on Rails: bad username / password? (535 Auth failed)

Jeff Wilkey picture Jeff Wilkey · May 14, 2015 · Viewed 8.9k times · Source

I just finished my ruby foundations coursework at Bloc and I'm starting to bury my head into rails development. Things were going smooth until I hit this snag with devise and confirmation emails. I tried googling and looking around at some other questions but couldn't really find any that gave me anything that I could pull from and apply to my situation.

I'm receiving the following error when signing up for an account.

Net::SMTPAuthenticationError in Devise::RegistrationsController#create

535 Authentication failed: Bad username / password


Error extracted from source around line #976

def check_auth_response(res)
  unless res.success?
    raise SMTPAuthenticationError, res.message
  end
end

From other posts I know you'll probably want to see that I have a config/initializers/setup_mail.rb file that looks like this:

if Rails.env.development?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address:        'smtp.sendgrid.net',
    port:           '587',
    authentication: :plain,
    user_name:      ENV['SENDGRID_USERNAME'],
    password:       ENV['SENDGRID_PASSWORD'],
    domain:         'heroku.com',
    enable_starttls_auto: true
  }
end

And here's an application.yml file EXAMPLE:

SENDGRID_PASSWORD: 
SENDGRID_USERNAME:
SECRET_KEY_BASE:

also I have the following in my config/environments/development.rb before anybody suggests it:

config.action_mailer.default_url_options = { host: 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
# Override Action Mailer's 'silent errors' in development
config.action_mailer.raise_delivery_errors = true

If there's any other files that you'd like to see let me know and I'll add them to this post.

Answer

eddiezane picture eddiezane · May 14, 2015

Congrats and welcome to the world of hacking on cool things.

The error you are getting means that the SendGrid server received a bad username + password combo.

Chances are your environment variables are empty and your application.yml file isn't being loaded properly with your SendGrid username + password.

You can confirm this by printing them out somewhere in your code. In a controller works.

puts "SENDGRID_USERNAME: #{ENV['SENDGRID_USERNAME']}"
puts "SENDGRID_PASSWORD: #{ENV['SENDGRID_PASSWORD']}"

I'd suspect that they are nil.

I'd recommend reading https://quickleft.com/blog/simple-rails-app-configuration-settings/ about how to get them sourced into your app.

Please let me know if you need any more help!