Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby sending mail via gmail smtp

Tags:

email

ruby

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.

like image 508
Krisztián Dudás Avatar asked Nov 25 '15 13:11

Krisztián Dudás


People also ask

Can I use Gmail SMTP server for sending mail?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.

How do I use SMTP in Ruby on Rails?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.


1 Answers

If you're not using 2 Factor Authentication

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.

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 => '', :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

like image 58
rainkinz Avatar answered Sep 18 '22 19:09

rainkinz