Josemafuen
Josemafuen

Reputation: 642

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:            '[email protected]',
      password:             'yyyyy',
      authentication:       'plain',
      enable_starttls_auto: true  }

My 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

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 [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/

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.

Upvotes: 2

Views: 2363

Answers (3)

nitish
nitish

Reputation: 1

Making any changes in the config needs to restart the server. After restarting the server the email will be sent successfully.

Upvotes: 0

hezanathos
hezanathos

Reputation: 94

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!

Upvotes: 3

Josemafuen
Josemafuen

Reputation: 642

It seems That the connection was being refused. Restarting the server solved the problem.

Upvotes: 1

Related Questions