发送Gmail时Rails4:SMTPAuthenticationError

时间:2014-12-27 07:32:25

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

虽然我试图添加"忘记密码"功能参考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'

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

\应用\模型\ 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
.
.

\应用\控制器\ 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。

\设置\环境\ 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  }
.
.

请告诉我如何避免此错误。

1 个答案:

答案 0 :(得分:0)

如果您使用的是gmail帐户(以gmail.com结尾),domain的{​​{1}}部分应更改如下:

config.action_mailer.smtp_settings

如果您使用带有example.com域名的Google Apps应用帐户,则只需将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。

相关问题