Running into SMTP error when trying to send email in RoR app

那年仲夏 提交于 2019-12-17 16:08:20

问题


I've been desperately trying to send an email using the Action Mailer class in RoR, and even though the terminal shows that a message is being sent, it keeps returning with this error:

 et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS

from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'...

Here's what my mailer class looks like within app/mailers/my_mailer.rb:

class MyMailer < ActionMailer::Base
  default from: "from@example.com"

  def welcome_email(user) 
    @user = user

        mail(to: user.email,
             from: 'example_email@gmail.com',
             subject: 'Welcome!')
  end
end

And here's what my config/application.rb file looks like:

config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {

        address:              'smtp.gmail.com',
        port:                 587,
        #domain:               'gmail.com',
        user_name:            'example_email@gmail.com',
        password:             'my_password',
        authentication:       'plain',
        enable_starttls_auto: true  

        }

        config.action_mailer.default_url_options = {
            :host => "localhost",
            :port => 3000
        }

in my config/environments/development.rb file I also have these two lines:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

My problem is that whenever I start the rails console (working in development, have no idea how to run in production), assign a user's email to the user variable then enter the command: MyMailer.welcome_email(user).deliver

the terminal says:

Rendered my_mailer/welcome_email.html.erb (11.7ms)
  Rendered my_mailer/welcome_email.text.erb (0.5ms)

Sent mail to abhas.arya@yahoo.com (923.1ms)
Date: Fri, 08 Aug 2014 13:54:38 -0400
From: abhas.aryaa@gmail.com
To: abhas.arya@yahoo.com
Message-ID: <53e50ededebb2_2b0b8082dbf8507c5@Abhass-MacBook-Pro.local.mail>
Subject: Welcome!
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

but then renders the error. My views are simple enough that I didn't think I needed to include them here. I've desperately tried everything, including unlocking google captcha but still nothing works. I'd appreciate any help, all I want is for an email to get sent to the proper inbox. I'd love you forever if you can solve this issue!!!


回答1:


The reason that you are seeing this error is that Google has blocked your IP. You will need to enable authorization for this IP.

Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.

Then again try to send email from the application, it should work. You will need to send email from your app within 10 min of visiting the above link. By visiting above link, Google will grant access to register new apps for 10 min.




回答2:


I solved by doing the following:

1) Log into the Google Apps Admin portal and enable Less Secure Apps by checking "Allow users to manage their access to less secure apps" under Security preferences.

2) Login into the account that will serve as the mailer and turn "Allow less secure apps" to ON.

Most of the answers available on-line refer to step 2, but you cannot configure at a user level without the Admin turning the feature on.




回答3:


This one solved the issue for me after some investigation:

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.




回答4:


I resolved my issue by doing this.

config.action_mailer.default :charset => "utf-8"
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'mysite.com',
    user_name: myemail@gmail.com,
    password: mypassword,
    authentication: 'plain',
    enable_starttls_auto: true
}

As google will try to block your sign-in if you have turned off access for less secure apps in your accounts settings. Therefore, follow this link and "Turn on" access for less secure apps.



来源:https://stackoverflow.com/questions/25209676/running-into-smtp-error-when-trying-to-send-email-in-ror-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!