Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Sending emails with pure Ruby

I have problems with sending emails with pure Ruby. Here is how my script looks like:

Mail.defaults do
  delivery_method(
    :smtp,
    address: 'smtp.sendgrid.net',
    port: 587,
    user_name: 'sendgrid_username',
    password: 'sendgrid_password',
    authentication: :login,
    enable_starttls_auto: false,
    openssl_verify_mode:  "none"
  )
end

Mail.deliver do
  from     'Test Email'
  to       '[email protected]'
  subject  'Here is the image you wanted'
  body     "Test Email"
end

This script raise the following error:

/Users/mateuszurbanski/.rubies/ruby-2.7.2/lib/ruby/2.7.0/net/smtp.rb:975:in `check_auth_response': 535 Authentication failed: Bad username / password (Net::SMTPAuthenticationError)

I'm using credentials from one of my Ruby on Rails project and they are fine. Any ideas?

Upvotes: 0

Views: 134

Answers (2)

Vasfed
Vasfed

Reputation: 18454

Sendgrid recently transitioned to api keys and will reject plain auth, see details here. Your old credentials while still being valid may not be accepted.

Generate a new api key in sendgrid and use it in place of password. Username will be apikey

Upvotes: 1

Anuj
Anuj

Reputation: 1940

Instead of using your SendGrid username and password in this config, use the following,

user_name: 'apikey',
password: '<sendgrid_api_key>',

This works for me when defining the smtp settings for ActionMailer, so it should work here as well.

Upvotes: 0

Related Questions