Techioz Blog

Railsメーラーが送信されない

概要

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  }

私のアプリ/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にあるメーラーを呼び出すメソッド

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 アカウントへのアクセスを許可しているため、問題は別のものであるはずです。何が問題なのかをデバッグする方法がわかりません。

解決策

メールのデバッグを改善するには、bang メソッドのDeliver_now! を試してみてください。例外が発生するかどうかを確認します (パスワードの問題など)。