Ruby sending mail via gmail smtp

I'm creating a ruby script that checks the response status of an url and if it equals with 504, it sends a mail to another email address. For some reason, I get this: /usr/lib/ruby/1.9.1/net/smtp.rb:960:in 'check_auth_response': 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbv9z (Net::SMTPAuthenticationError) I quadra checked the authentication data and they are valid. Maybe there can be something wrong in the code:

require 'mail'

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :user_name            => '<myusername>',
            :password             => '<mypassword>',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }

Mail.defaults do
  delivery_method :smtp, options
end

Mail.deliver do
       to '[email protected]'
     from '[email protected]'
  subject 'Test'
     body 'Hurray!!! Test email!'
end

Oh also, I got the notice from google that a less secure app tried to access my account, so I set up that less secure apps can use my account.

Upvotes: 16

Views: 10275

Answers (1)

rainkinz
rainkinz

Reputation: 10394

If you are using 2 Factor Authentication

You need to create an app specific password for your application. Follow these steps:

  1. Go to gmail

  2. in the top right corner click on your profile icon and select 'My Account'

  3. Click on 'Sign in & Security'

  4. Scroll down the 'Sign in & Security' page a bit and there is a section called 'App Passwords' Click on that.

  5. You should see a dropdown labelled 'Select App'. Select Mail.

  6. For the 'on my device' dropdown select 'Other' and type in commandline or whatever you want to call the app.

  7. Click 'Generate'. A password will be generated. Copy that password and replace the password you were using in your options hash with the generated password:

    options = { :address => "smtp.gmail.com", :port => 587, :user_name => '', :password => '<generated_password_for_app>', :authentication => 'plain', :enable_starttls_auto => true }

That should be it. I just tried this and it worked for me.

Also make sure your username is your full gmail email address.

You can also find the 'Official docs' here: https://support.google.com/accounts/answer/185833?hl=en

If you're not using 2 Factor Authentication

NOTE: I think this answer is now out of date as per @EricDuminil

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. Which apparently means this answer no longer applies. Too bad.. –

Go to this link:

https://www.google.com/settings/security/lesssecureapps

And select:

"Access for less secure apps"

as per:

https://support.google.com/accounts/answer/6010255?hl=en

In this case you would use your normal email and password to connect.

Upvotes: 25

Related Questions