legendary_rob
legendary_rob

Reputation: 13012

ActionMailer not delivering mail in development

I am trying to build a password reset email. I am Using Rails 3.2.16, and Ruby 1.9.3p327, I have read a ton of answers to this question and tried pretty much everything. I have also gone through the action mailer basics guide, and as far as i can see this should be working but its just not going well. Ill do a step by step guide of how i set it up.

firstly since i am trying to get this working in development, within development.rb Note: I have reset the application each time i edited the development.rb file.

#this is all the action mailer settings i have defined in development.rb
config.action_mailer.raise_delivery_errors = true # Set to true so that errors would be visible.
config.action_mailer.perform_deliveries = true # I read about this possible fix on SO
config.action_mailer.default_url_options = {
  host: "boogle.dev"
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.office365.com",
  :port                 => 587,
  :domain               => "mpowered.co.za",
  :user_name            => "[email protected]",
  :password             => "password",
  :authentication       => :login,
  :enable_starttls_auto => true
}

My notifier class which inherits from ActionMailer

class Notifier < ActionMailer::Base
  default from: "Mpowered - BEEtoolkit <[email protected]>"

  def deliver_password_reset_email(user)
    @edit_password_reset_url = edit_password_reset_url(user.perishable_token)
    @name = user.name

    mail(
      subject:       "Password Reset Instructions",
      to:            user.email,
      date:          Time.now,
      content_type:  "text/html")
  end
end

Within my User model i have set up the method which will send the mail along with setting up of a perishable_token

def deliver_password_reset_instructions!
  reset_perishable_token!
  Notifier.deliver_password_reset_email(self)
end

The Passwords reset controller is set up like this:

class PasswordResetsController < ApplicationController
  before_filter :require_no_user
  before_filter :load_user_using_perishable_token, :only => [ :edit, :update ]

  def new
  end

  def create
    @user = User.find_by_email(params[:email])
    if @user
      @user.deliver_password_reset_instructions!
      flash[:notice] = "Instructions to reset your password have been emailed to you"
      render action: :new
    else
      flash.now[:error] = "No user was found with email address #{params[:email]}"
      render action: :new
    end
  end

  def edit
  end

  def update
    @user.password = params[:password]
    # Only if your are using password confirmation
    @user.password_confirmation = params[:password]

    # Use @user.save_without_session_maintenance instead if you
    # don't want the user to be signed in automatically.
    if @user.save
      flash[:success] = "Your password was successfully updated"
      redirect_to @user
    else
      render action: :edit
    end
  end


  private

  def load_user_using_perishable_token
    @user = User.find_using_perishable_token(params[:id])
    unless @user
      flash[:error] = "We're sorry, but we could not locate your account"
      redirect_to root_url
    end
  end
end

I added resources to my routes:

resources :password_resets, :only => [ :new, :create, :edit, :update ]

My views are simple: in app/views/password_resets/new.haml.html

%br
= form_tag password_resets_path, class: 'form-inline' do
  %legend Forgotten Password
  %p Enter your email address and instructions to reset your password will be emailed to you:
  %span.span1
    = label_tag :email, 'Email'
  = text_field_tag :email
  = submit_tag 'Reset my password', class: 'btn'
%br

So this should send the mail once you submit a valid email. You should then receive an email with this content: app/views/notifier/password_reset_instructions.html.haml

%h1 Password Reset Instructions

%p
  A request to reset your password has been made. If you did not make
  this request, simply ignore this email. If you did make this
  request, please follow the link below.

= link_to "Reset Password!", @edit_password_reset_url

The link should bring you to a form where you can then save a new password and password confirmation.

app/views/password_resets/edit.html.haml

- if @user
  = form_for @user, :url => password_reset_path, :method => :put do |f|
    %legend Change My Password
    %p Please select a new password for your account
    .span8
      = f.field :password, :field_type => :password_field, :label => "New password"
      = f.field :password_confirmation, :field_type => :password_field
    .clearfix
    = f.submit "Update my password", class: 'btn'
- else
  %h3 We couldn't identify your reset code
  %p We're sorry, but we could not locate any accounts with reset codes that matched yours.
  %p If you are having difficulties resetting your password, try copying and pasting the URL from your password reset email into your browser or restarting the password reset process.

to which you can save your new password and then login once more.. this is what i have set up in the app. but everytime i try send it by following the system, it says the email was sent but nothing ever comes. I have also tried loading up a user in the console and then running u.deliver_password_reset_instructions!

and i get this:

enter image description here

But still no email in my inbox. I have currently set the email address in the notifier to my own personal one so no matter what valid email address is requested, the email should come to me.

I have been hitting walls for the last 12 hours and have no idea where to turn. i am hoping i have made a balls up that a fresh pair of eyes can catch.

Upvotes: 0

Views: 269

Answers (1)

anusha
anusha

Reputation: 2135

You need to add .deliver when calling Mailer method like this

def deliver_password_reset_instructions!
  reset_perishable_token!
  Notifier.deliver_password_reset_email(self).deliver
end

Hope this helps

Upvotes: 3

Related Questions