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

Ruby on-RailsRuby on-Rails-3Gmail

Ruby on-Rails Problem Overview


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)

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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
}

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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.

Solution 5 - Ruby on-Rails

Solution 6 - Ruby on-Rails

I had the same problem.

Solution:

Solution 7 - Ruby on-Rails

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!

Solution 8 - Ruby on-Rails

To resolve this issue:

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.

Solution 9 - Ruby on-Rails

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!

Solution 10 - Ruby on-Rails

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.

Solution 11 - Ruby on-Rails

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".

Solution 12 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneagorView Question on Stackoverflow
Solution 1 - Ruby on-RailsGee-BeeView Answer on Stackoverflow
Solution 2 - Ruby on-RailseagorView Answer on Stackoverflow
Solution 3 - Ruby on-RailsA H KView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSorry-Im-a-N00bView Answer on Stackoverflow
Solution 5 - Ruby on-RailsaashishView Answer on Stackoverflow
Solution 6 - Ruby on-RailsBenyamin JafariView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAlejandro SherwellView Answer on Stackoverflow
Solution 8 - Ruby on-RailsYash DubeyView Answer on Stackoverflow
Solution 9 - Ruby on-RailsubugnuView Answer on Stackoverflow
Solution 10 - Ruby on-RailscooxyView Answer on Stackoverflow
Solution 11 - Ruby on-RailsShrinivasView Answer on Stackoverflow
Solution 12 - Ruby on-RailsMirza VuView Answer on Stackoverflow