【问题标题】:Rails4: SMTPAuthenticationError when sending GmailRails4:发送Gmail时出现SMTPAuthenticationError
【发布时间】:2014-12-27 07:32:25
【问题描述】:

虽然我尝试通过引用 RAILSCAST #247 添加“忘记密码”功能,但错误消息“我们很抱歉,但出了点问题。”在生产环境(不是 Heroku)的浏览器上显示。

访问下面的链接后发生了同样的错误。 http://www.google.com/accounts/DisplayUnlockCaptchahttps://www.google.com/settings/security/lesssecureapps

stdout.log 如下;

Started POST "/password_resets" for 12.345.67.89 at 2014-12-27 09:10:24 +0000
Processing by PasswordResetsController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "email"=>"xxx@xxx.com", "commit"=>"reset PW"}
  Rendered user_mailer/password_reset.text.erb (1.1ms)

Sent mail to xxx@xxx.com (1617.4ms)
Completed 500 Internal Server Error in 1775ms

Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=xxx
):
  app/models/user.rb:36:in `send_password_reset'
  app/controllers/password_resets_controller.rb:6:in `create'

导致错误的这些文件的内容如下。

\app\models\user.rb

class User < ActiveRecord::Base
.
.
  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!(validate: false)
    UserMailer.password_reset(self).deliver    #line 36
  end
.
.

\app\controllers\password_resets_controller.rb

class PasswordResetsController < ApplicationController
.
.
  def create
    user = User.find_by_email(params[:email])
    user.send_password_reset if user          #line 6
    redirect_to root_url, :notice => "Email sent with password reset instructions."
  end
.
.

我在 production.rb 中添加了一些设置以通过参考 RailsGuides 来使用 Gmail。

\config\environments\production.rb

.
.
config.action_mailer.default_url_options = { :host => 'example.com' } #change to hosting setting
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'example.com', #change to hosting setting
  :user_name            => 'xxx@gmail.com',
  :password             => 'password',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }
.
.

请告诉我如何避免这个错误。

【问题讨论】:

  • google mail 默认阻止 smtp auth,尝试在设置 gmail 帐户中找到有关此的内容。
  • 感谢您的建议,@Зелёны。它按原样工作。好像反射UnlockCaptchalesssecureapps花了很长时间。

标签: ruby-on-rails ruby ruby-on-rails-4 heroku gmail


【解决方案1】:

如果您使用的是 gmail 帐户(以 gmail.com 结尾),config.action_mailer.smtp_settingsdomain 部分应更改如下:

config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => 'xxx@gmail.com',
  :password             => 'password',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

如果您使用的是带有 example.com 域的 Google Apps 帐户,则只需将 domain 设置为 example.com。

【讨论】:

  • 感谢您的建议,@sjaime。虽然我不尝试你的答案,但它有效。好像反射UnlockCaptchalesssecureapps花了很长时间。
猜你喜欢
  • 2022-08-21
  • 2015-01-07
  • 1970-01-01
  • 2016-06-09
  • 2022-11-03
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
相关资源
最近更新 更多