【问题标题】:Rails mailer not sendingRails 邮件程序未发送
【发布时间】:2018-05-04 20:12:36
【问题描述】:

我正在尝试通过我的 Rails 应用程序发送“密码重置”邮件。但问题是尽管我的日志显示它已发送,但电子邮件从未到达。

这是 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  }

我的应用程序/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 帐户,所以问题肯定是另一回事。我不知道如何调试出了什么问题。

【问题讨论】:

  • 同时检查您的 gmail 发件箱
  • 它是空的,我会编辑问题

标签: ruby-on-rails ruby email gmail actionmailer


【解决方案1】:

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

方法:ActionMailer::MessageDelivery#deliver_now!

定义于: actionmailer/lib/action_mailer/message_delivery.rb

现在交付! ⇒ 对象

发送电子邮件而不检查 perform_deliveries 和 raise_delivery_errors,因此请谨慎使用。

Notifier.welcome(User.first).deliver_now!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多