ruby-on-railsrubyemailgmailactionmailer

Rails mailer not sending


I'm trying to send a 'password-reset' mail through my Rails application. But the problem is that the email never arrives although my logs say it is sent.

Here's the config in 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  }

My 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

And the method that calls the mailer in 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

The views and everything are ok because, as I said, the logs say that the email was sent successfully, I must be missing some configuration I guess. Here are the logs:

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/

Moreover, the outbox of the sender gmail is empty but I have allowed less secure apps to access my gmail account, so the problem must be another thing. I don't know how to debug what's going wrong.


Solution

  • For a better mail debugging you can try the bang method deliver_now! and see if it raises any exeption (for example password issue).

    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!