9
votes

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.

3

3 Answers

10
votes

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!

6
votes

Two-Factor Authentication is required as of Q4 2020, and all Twilio SendGrid API endpoints will reject new API requests and SMTP configurations made with a username and password via Basic Authentication.

I received a similar issue from an app I've been running for the last couple years. From now on, basic auth doesn't work, and you'll need to use an alternative auth mechanism. Heroku sendgrid auto-configuration on installation has not yet been updated to reflect this.

Source: https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys/

0
votes

In my case the error was to use as username the id of my apikey, but this is wrong, the correct value user_name is 'apikey', (Literally 'apikey' string), as they say in their integration example, https://sendgrid.com/docs/for-developers/sending-email/rubyonrails/ https://app.sendgrid.com/guide/integrate/langs/smtp

ActionMailer::Base.smtp_settings = {
:user_name => 'apikey', # This is the string literal 'apikey', NOT the ID of your API key
:password => '<SENDGRID_API_KEY>', # This is the secret sendgrid API key which was issued during API key creation
:domain => 'yourdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}