【问题标题】:Running into SMTP error when trying to send email in RoR app尝试在 RoR 应用程序中发送电子邮件时遇到 SMTP 错误
【发布时间】:2014-10-02 07:09:15
【问题描述】:

我一直在拼命尝试使用 RoR 中的 Action Mailer 类发送电子邮件,即使终端显示正在发送消息,它仍然会返回此错误:

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

这是我的邮件程序类在 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

这是我的 config/application.rb 文件的样子:

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
        }

在我的 config/environments/development.rb 文件中,我也有这两行:

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

我的问题是,每当我启动 Rails 控制台(在开发中工作,不知道如何在生产中运行)时,将用户的电子邮件分配给用户变量,然后输入命令:MyMailer.welcome_email(user).deliver

终端说:

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

但随后呈现错误。我的观点很简单,我认为我不需要将它们包括在这里。我已经拼命尝试了一切,包括解锁谷歌验证码,但仍然没有任何效果。我会很感激任何帮助,我想要的只是将电子邮件发送到正确的收件箱。如果你能解决这个问题,我会永远爱你!!!

【问题讨论】:

    标签: ruby-on-rails ruby email gmail


    【解决方案1】:

    您看到此错误的原因是 Google 已阻止您的 IP。您需要为此 IP 启用授权。

    转到http://www.google.com/accounts/DisplayUnlockCaptcha 并点击继续。

    然后再次尝试从应用程序发送电子邮件,它应该可以工作。您需要在访问上述链接后 10 分钟内从您的应用程序发送电子邮件。通过访问上述链接,Google 将授予注册新应用 10 分钟的访问权限。

    【讨论】:

    • 谢谢,我试过了,但不幸的是它仍然不起作用,并且显示完全相同的错误
    • 非常感谢 Raj,我点击了错误日志中的链接并暂时启用了对安全性较低的应用程序的访问,并且成功了!你这个摇滚老兄!
    【解决方案2】:

    我通过以下方式解决了:

    1) 登录 Google Apps 管理门户并通过选中“安全首选项”下的“允许用户管理对安全性较低的应用程序的访问”来启用安全性较低的应用程序。

    2) 登录将用作邮件程序的帐户并将“允许不太安全的应用程序”设置为开启。

    大多数在线可用答案都参考第 2 步,但如果管理员未打开该功能,则无法在用户级别进行配置。

    【讨论】:

    • 我认为有必要提一下“admin.google.com 仅用于 Google Workspace 帐户。普通 Gmail 帐户不能用于登录 admin.google.com”
    【解决方案3】:

    经过一番调查,这个问题为我解决了:

    点击https://www.google.com/settings/u/0/security/lesssecureapps

    通过使用您在 smtp 配置中提供的电子邮件地址登录,在此处启用对安全性较低的应用程序的访问。

    【讨论】:

      【解决方案4】:

      我通过这样做解决了我的问题。

      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
      }
      

      如果您在帐户设置中关闭了安全性较低的应用的访问权限,Google 会尝试阻止您的登录。因此,请按照此link 和“开启”访问权限来访问不太安全的应用程序。

      【讨论】:

        猜你喜欢
        • 2018-02-25
        • 2019-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-15
        相关资源
        最近更新 更多