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

12

103

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 => "[email protected]",
      :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)
Seize answered 8/8, 2013 at 11:29 Comment(1)
I've copied the url in error (google probably thought in advance for us) to browser. Entered my password and in the panel I've seen switched the possibility to make requests not only from trusted sites.Cotangent
C
260

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 ;)

Cymose answered 28/11, 2013 at 9:41 Comment(7)
That's actually is the correct way to solve it (although it may put your account at risk). But it works.Morpheus
Also do not forget allow account-access here:: google.com/settings/security/lesssecureappsWicket
I was able to avoid the lesssecureapps setting by using multifactor authentication and enabling an app-specific password for my rails server.Rama
Probably better to use a service like Mandrill for sending emails in a production environmentBobbibobbie
I have turned on lesssecureapps account setting and went to DisplayUnlockCaptcha page and pressed OK, sent email through rails but received the same error Net::SMTPAuthenticationError ...ContinueSignIn.... Then I tried to do the same after 10 minutes and it started working.Gondola
I'm guessing I only have to do this every time I deploy, am I correct?Hygroscope
@ChrisBeck, can you explain the whole process ?Nowhere
S
27

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 => "[email protected]",
      :password => "my_email_password",
      :authentication => :plain,
      :enable_starttls_auto => true
}
Seize answered 8/8, 2013 at 14:54 Comment(3)
Thank you! This problem was causing me a lot of headache, and in the end what I needed to do was change my google account password after using these settings and setting the allow less secure apps option in the google account settings.Perea
After setting 'allow less secure apps' and tell google to trust my device (server's IP) and waiting for over 24 hrs... a simple change of password fixed my gmail authentication issues. someone at google should probably fix that. ;)Nymphomania
I was facing the same issue. I had already enabled lessecureapps but it wont fix. I simply changed my password and it worked like a charm. :)Plexiglas
P
25

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 => "[email protected]",
      :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.

Prologue answered 13/10, 2014 at 19:41 Comment(1)
Is there a way around doing this? Is it possible to register you app with google, for instance?Bramwell
C
15

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.

Culmiferous answered 9/9, 2013 at 3:17 Comment(1)
Direct link to "clear the CAPTCHA" page: accounts.google.com/DisplayUnlockCaptchaKrell
C
15

go to following link and turn on https://www.google.com/settings/security/lesssecureapps

Cartel answered 27/2, 2015 at 8:53 Comment(0)
M
7

I had the same problem.


Solution:

Monocular answered 9/4, 2018 at 9:10 Comment(0)
D
3

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!

Dambro answered 2/6, 2016 at 16:4 Comment(0)
V
3

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.

Variorum answered 28/7, 2020 at 20:6 Comment(1)
This fixed it for me thanks for the info.Diameter
P
1

The accepted answer seems very old, I don't know if at that time the followin (better) solution was existing:

Now, sending emails works perfectly!

Puerile answered 11/7, 2018 at 15:8 Comment(0)
D
0

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: '[email protected]' 
gmail_password: "your_example_email_password_here"

You must use your email and password for authentication in the file.

Donkey answered 8/8, 2016 at 17:25 Comment(1)
config.action_mailer.default_url_options = { host: "localhost:3000" }Donkey
S
0

I also faced the problem, and after some research in Gmail setting, I found the solution:

  1. In gmail, go to settings.

  2. Select "Forwarding and POP/IMAP" tab.

  3. In the IMAP access section, select "Enable IMAP".

Squinty answered 5/7, 2017 at 10:59 Comment(0)
O
-1

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.

Orthotropous answered 22/7, 2016 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.