Rails 邮件程序未发送

Rails mailer not sending

我正在尝试通过我的 Rails 应用程序发送 'password-reset' 邮件。但问题是,尽管我的日志显示已发送,但电子邮件从未到达。

这是 config/environments/development.rb

中的配置
config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      user_name:            'xxxxx@gmail.com',
      password:             'yyyyy',
      authentication:       'plain',
      enable_starttls_auto: true  }

我的 app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  default from: "xxxxx@gmail.com"

  def password_reset(user)
    @user = user
    mail :to => user.email, :subject => "Password Reset"
  end
end

以及app/models/user.rb

中调用mailer的方法
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_now
  end

视图和一切都很好,因为正如我所说,日志显示电子邮件已成功发送,我想我一定是缺少一些配置。这是日志:

Sent mail to zzzzz@hotmail.com (2007.7ms)
Date: Tue, 21 Nov 2017 12:10:46 +0100
From: xxxxx@gmail.com
To: zzzzz@hotmail.com
Message-ID: <5a1409b627bb8_3ca86e9450429578@DESKTOP-85KSOM4.mail>
Subject: Password Reset
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Sigue el siguiente enlace para restablecer la contrase=C3=B1a
http://localhost:3000/password_resets/w0F77qrpHbM9HvXaFElWGA/edit
Ignora este email si no pediste el cambio.

Redirected to http://localhost:3000/

此外,发件人gmail的发件箱是空的,但我允许安全性较低的应用程序访问我的gmail帐户,所以问题一定是另一回事。我不知道如何调试出了什么问题。

为了更好的邮件调试,您可以尝试 bang 方法 deliver_now! 看看它是否引发任何异常(例如密码问题)。

Method: ActionMailer::MessageDelivery#deliver_now!

Defined in: actionmailer/lib/action_mailer/message_delivery.rb

deliver_now! ⇒ Object

Delivers an email without checking perform_deliveries and raise_delivery_errors, so use with caution.

Notifier.welcome(User.first).deliver_now!

似乎连接被拒绝了。重启服务器解决问题。