ruby Rails Mailer未发送

vtwuwzda  于 9个月前  发布在  Ruby
关注(0)|答案(3)|浏览(111)

我试图通过Rails应用程序发送一封“密码重置”邮件,但问题是,尽管我的日志显示邮件已经发送,但邮件从未到达。
下面是config/environments/development.rb中的配置

config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      user_name:            '[email protected]',
      password:             'yyyyy',
      authentication:       'plain',
      enable_starttls_auto: true  }

字符串
我的app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  default from: "[email protected]"

  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 [email protected] (2007.7ms)
Date: Tue, 21 Nov 2017 12:10:46 +0100
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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帐户,所以问题肯定是另一回事,我不知道如何调试出了什么问题。

xlpyo6sf

xlpyo6sf1#

为了更好地调试邮件,你可以尝试bang方法deliver_now!,看看它是否会引发任何异常(例如密码问题)。
方法:发送邮件::MessageDelivery#deliver_now!
定义于:actionmailer/lib/action_mailer/message_delivery.rb

deliver_now!deliver对象

发送电子邮件时不检查perform_delivery和raise_delivery_errors,因此请谨慎使用。
Notifier.welcome(User.first).deliver_now!

tzcvj98z

tzcvj98z2#

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

cig3rfwq

cig3rfwq3#

在配置中进行任何更改都需要重新启动服务器。重新启动服务器后,电子邮件将成功发送。

相关问题