Net::SMTPAuthenticationError using Rails ActionMailer - ruby-on-rails

I just fired up a Rails app I was building back in January which would send emails via Gmail's smtp protocol. Today I get a Net::SMTPAuthenticationError with 530-5.5.1 Authentication Required. I googled this and tried all the suggestions like re-logging into Gmail, unlocking Captcha, and changing my password. None of these worked.
Here's my development.rb code:
config.action_mailer.default_url_options = { host: "localhost:3000" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
UPDATE
well this is strange. I changed my server to smtp.yahoo.com and I'm getting the same Rails error.
UPDATE 2
I also got a new ISP since the last time I tried this app. Could that be a problem?

I think this could be that Google is suspicious of the website accessing your account (I had something similar recently too).
Login into your gmail account and look towards the top of the page for a warning banner indicating this suspicious activity concern.
Click that warning banner at the top of the page. It should take you to a page that you can inform Gmail (by the click of a button... and maybe a word verification request) that this really was you trying to access your account. There should even be a novel little map of the location of the entity (in this case, your server) that tried to access your account.
They should have sent you an email about this.
The email should look something like this:
Someone recently used your password to try to sign in to your Google
Account th.good#gmail.com. This person was using an application such
as an email client or mobile device.
We prevented the sign-in attempt in case this was a hijacker trying to
access your account. Please review the details of the sign-in attempt:
... If this was you, and you are having trouble accessing your
account, complete the troubleshooting steps listed at
http://support.google.com/mail?p=client_login
Hopefully that helps.

Just change the authentication type:
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => ENV["GMAIL_USERNAME"],
:password => ENV["GMAIL_PASSWORD"]
:authentication => 'login',
:enable_starttls_auto => true
}
and make sure you have values for ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"]. It should be a real gmail email-id (username) and its password. It will work fine.
Hope it helps :)

I think you are using Figaro gem. You have to setup ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] in application.yml file. Check documentation of Figaro Gem.
Check you setting too.
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'my_nick#gmail.com',
:password => 'secret_password',
:authentication => 'login',
:enable_starttls_auto => true
}

Well, I had to add a Windows partition next to Linux on my laptop. After re-installing, the problem went away...so I don't know what the problem was.

Related

Heroku Action Mailer with Gmail, sends email only once = Net::SMTPAuthenticationError

Strange behaviour I have. I'm using gmail to send my emails in my Rails app. I have my gmail configured to accept less secure apps.
However I send one email and then when I go to my google account it prompts me to restore the account via my phone.
After that, my app no longer sends email and I get this in my Heroku logs:
Net::SMTPAuthenticationError (534-5.7.9 Please log in with your web browser and then try again.
My production.rb is set up this way:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'xxx#gmail.com',
:password => 'xxxxxx',
:authentication => "plain",
:enable_starttls_auto => true,
}
Anybody any ideas?
I think you have to set the domain to gmail.com
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "xxxxxxx",
:authentication => :plain,
:enable_starttls_auto => true
}
If it worked then ok, if not try to change the :authentication to :login.
Advice: Check Gmail's security events too and double-check that all security settings are intact.
So I had several problems. The one that was caused by my inattention was that I didn't read Figaro gem docs properly and I wasn't sending out the proper info to Heroku with figaro heroku:set -e production.
Secondly my account would go into lockdown after I created it and sent first email. Steps to do:
enable two-step verifiation
generate app specific keys
put them in your config/application.yml if you're using Figaro
push and run figaro command
you might have to clear CAPTCHA on gmail see here
Follow the coment by #Mohamed
Advice: Check Gmail's security events too and double-check that all security settings are intact.
LINK: https://www.google.com/settings/security/lesssecureapps

gmail blocking rails app from sending email

I'm pretty sure I have everythign configured right, but I keep getting 'Net::SMTPAuthenticationError: 535-5.7.8 Username and Password not accepted. Learn more at' error when trying to send mails through my rails app. Also, there is only a blank space after the 'at' in the error message so I don't even know where to look for more information. I am also 100% sure the password and email are correct.
in my config/development.rb (I also have this in application.rb)
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'username#gmail.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true
}
I have allowed less secure apps access on the actual gmail account.
Google blocks all suspicious login attempts. By using app password, I was able to solve the problem:
Enable 2-Step Verification if you haven't done so.
Go to App passwords (Google will ask you to login again to verify here)
Create a new App password: Choose Other (Custom name) under Select app, then enter app's name (any name you want)
Click generate, a popup will be shown with a password. Use that password in your rails configuration instead of your google password.
This is the config that works for me with Google apps for business. Try changing your port to 465
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
tls: true,
address: ENV["EMAIL_URL"],
port: 465,
domain: ENV["EMAIL_DOMAIN"],
user_name: ENV["EMAIL_USER_NAME"],
password: ENV["EMAIL_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true }

Rails 4, how to correctly configure smtp settings (gmail)

I am trying to create a contact form in Rails 4. I did some digging around here and was able to get most of the stuff to work. (followed #sethfri's work here Contact Form Mailer in Rails 4)
Right now I am able to fill out my form box and hit send. In my rails server it says the mail was outbound to my email address, but I don't receive anything in my gmail box, so I think my smtp settings aren't right. My smtp settings are:
...config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.net",
:user_name => "mygmailusername#gmail.com",
:password => "myGmailPassword",
:authentication => "plain",
:enable_starttls_auto => true
}
Also I added in
.../config/initializers/smtp_settings.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.net",
:user_name => "gmailuser#gmail.com",
:password => "gmailPassword",
:authentication => "plain",
:enable_starttls_auto => true
}
What am I missing/doing wrong? I've played around with a couple things (changed default_url to port 1025, changed :port => "587" to :port => 587) with no success.
Thanks for the help!
You have to set the domain correctly. Currently in the code posted its "mydomain.net". Change it to gmail.com if you want to sent it via gmail.
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'xyz#gmail.com',
password: 'yourpassword',
authentication: :plain,
enable_starttls_auto: true
}
If you run into errors like Net::SMTPAuthenticationError while using gmail for sending out emails (common for Google Apps accounts), visit your gmail settings and enable less secure apps to get the application working.
After few hours to search how to make this working for me, i find a way to make it work. For myself, i needed to make 2-Step Verification and use Gmail application password
When you enable 2-Step Verification (also known as two-factor authentication), you add an extra layer of security to your account. You sign in with something you know (your password) and something you have (a code sent to your phone).
Set up 2-Step Verification
Go to the 2-Step Verification page. You might have to sign in to your Google Account.
In the "2-Step Verification" box on the right, select Start setup.
Follow the step-by-step setup process.
An App password is a 16-digit passcode that gives an app or device permission to access your Google Account. If you use 2-Step-Verification and are seeing a “password incorrect” error when trying to access your Google Account, an App password may solve the problem. Most of the time, you’ll only have to enter an App password once per app or device, so don’t worry about memorizing it
How to generate an app password
Visit your App passwords page. You may be asked to sign in to your Google Account.
At the bottom, click Select app and choose the app you’re using.
Click Select device and choose the device you’re using.
Select Generate.
Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
Select Done
2020, Rails 6 updated answer:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
authentication: "plain",
enable_starttls_auto: true,
user_name: "blabla#gmail.com",
password: "blabla",
domain: "smtp.gmail.com",
openssl_verify_mode: "none",
}
Google recommends to use OAuth 2.0 for the login process. This configuration is "not so safe" for google, but they tolerate it. You have to allow "less safe connections" in your Google account settings or use the OAuth-way.
https://developers.google.com/identity/protocols/OAuth2
Their library for ruby is still alpha.
There seem to be some gems extending ActionMailer for OAuth, but I never used them.

Net::SMTPAuthenticationError when sending email from Rails app (on staging environment)

I am sending email from my Rails application. It works well on development environment, but fails on staging. I get the following error:
Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/ContinueSignIn?plt=AKgnsbtdF0yjrQccTO2D_6)
Note, that my I don't have a domain name for my staging.
Here are my settings in staging.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here:80" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80'
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => 'login'
}
Please, help.
Edit.
After adding :tls => true option I get
OpenSSL::SSL::SSLError (Unrecognized SSL message, plaintext connection?)
And then I changed port to 25 and now I get this (with 30 seconds delay):
Timeout::Error (execution expired)
I had the same problem: emails were sent from development, but not from production (where I was getting Net::SMTPAuthenticationError).
This drove me to conclusion that the problem was not with my app's configuration, but with Google.
Reason: Google was blocking access from unknown location (app in production)
Solution: Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps).
After this my app in production started sending emails ;)
Solved!
I simply changed password for my gmail account and somehow errors disappeared.
After dozen of changes, the final settings I ended up with are:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80',
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => :plain,
:enable_starttls_auto => true
}
This solution is working for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'localhost:3000',
:user_name => "xyz#gmail.com",
:password => "password",
:authentication => :plain,
:enable_starttls_auto => true
}
It's true that Google will block your sign-in attempt but
You can change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
go to following link and turn on
https://www.google.com/settings/security/lesssecureapps
The above solution provided the correct settings (which I already had) but did not solve the issue. After continued attempts, I kept getting the same error. Turns out, I had to "clear the CAPTCHA" from the web. See the gmail documentation for details.
You can also jump right to the "clear the CAPTCHA" page here.
I had the same problem.
Solution:
You can turn on less secure apps option (at here).
https://myaccount.google.com/lesssecureapps
And unlock Captcha (link):
https://accounts.google.com/DisplayUnlockCaptcha
A lot later but just in case it helps anyone.. Just called Google Apps Help Center and they instructed to change the lesssecureapps setting (as everyone else) but also to change the port to 465.
In my case, that did the trick!
To resolve this issue:
If you see: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.), then you need to allow less secure apps to login your google account.
To enable less secure apps login follow: https://myaccount.google.com/lesssecureapps?. But will allow all apps to login.
If you want to customize it refer : https://support.google.com/a/answer/6260879?hl=en
Then may be possible you will get Net::SMTPAuthenticationError (534-5.7.14), so to resolve this refer: pli=1http://www.google.com/accounts/DisplayUnlockCaptcha. After that click the continue from the page you get redirected. It will verify your Captcha and your app will get verified to use your google account to send emails.
NOTE: Please make sure you'r using correct credentials of your gmail account.
If you'r not willing to allow all the apps please refer: https://support.google.com/a/answer/6260879?hl=en. From the link go to Use alternatives to less secure apps, this will guide you to an alternative way to Allow Less Secure apps access to your google account.
The accepted answer seems very old, I don't know if at that time the followin (better) solution was existing:
Go to https://myaccount.google.com/u/0/apppasswords
Generate a new password for you app
Replace the personal password with the generated password in config.action_mailer.smtp_settings
Now, sending emails works perfectly!
Hello this also worked for me and so if someone is still having a problem try this out.
Make sure you have figaro in your gemfile.
To save sensitive information such as username and password as environment variables
gem 'figaro'
And in your config/environments/development.rb , paste the codes below
using smtp as method delivery
config.action_mailer.delivery_method = :smtp
SMTP settings for gmail
config.action_mailer.smtp_settings =
{
:address=> "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password=> ENV['gmail_password'],
:authentication=> "plain",
:enable_starttls_auto=>true
}
config.action_mailer.default_url_options = { host: "locahost:3000" }
In your config directory create a file called application.yml
and add the codes below.
gmail_username: 'example#gmail.com'
gmail_password: "your_example_email_password_here"
You must use your email and password for authentication in the file.
I also faced the problem, and after some research in Gmail setting, I found the solution:
In gmail, go to settings.
Select "Forwarding and POP/IMAP" tab.
In the IMAP access section, select "Enable IMAP".
I had the same problem and after some trial and errors, have come to this resolution which is an option to be enabled in google:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable 'Access for less secure apps' here by logging in with the email address you have provided in smtp configuration.

Using ActionMailer with a company Gmail account

I'm not sure if this belongs in server fault or here feel free to move it if it makes more sense somewhere else. I've seen the examples for setting up the smtp settings and using ActionMailer with Gmail and confirmed that they work for me.
Basically it looks like this for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
What I need to do now is to send an email to an address that isn't a plain Gmail account and yet, at it's core, is Gmail. My company uses whatever google's email service that allows you to use gmail but for the addresses to be listed as username#my.company.com rather than #gmail.com. I know for a fact that you can't simply log into our mail at the main gmail site so I assume our domain is different. Or something.
At the moment, when I simply use my own company user/pass I get an error message telling me that the user/pass was wrong. But I'm guessing the issue is that I'm trying to mail from the gmail variant of my username.
I have confirmed that our smtp server, as far as Thunderbird is concerned, is the normal gmail smtp, that our port is still 587, and that we are using TLS. What do I need to change here so that I can send an email to one of these addresses? Thanks.
I have my own domain setup at google for mail, the url to log into that directly is
http://mail.google.com/a/my.company.com
My rails app that sends mail through that account, has
:domain => "my.company.com"
as well as all the other fields you have.
"<user_name>" needs to be the whole email address, not just the user name.
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'busiess.com',
:user_name => 'username#company.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true

Resources