How can I customize Devise to send password reset emails using PostMark mailer
Asked Answered
C

3

19

I'm trying to get all of my system's email notifications under one umbrella using PostMarkApp and utilizing the Rails gems (postmark-rails, postmark-gem, and mail). I have successfully created a mailer that handles sending receipts for purchases but I haven't been able to receive emails for forgotten passwords. My development logs show that Devise sent the message but no email is received in my inbox and the PostMark credits are not decremented.

What's the best or easiest way to have Devise's mailers send through my PostMark account?

Snippet from config/environments/development.rb

config.action_mailer.delivery_method      = :postmark
config.action_mailer.postmark_settings    = { :api_key => "VALID_API_KEY_WAS_HERE" }
config.postmark_signature                 = VALID_POSTMARK_SIGNATURE_WAS_HERE

My Mailer that uses Postmark

class Notifier < ActionMailer::Base
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end
end

EDIT: SOLUTION to my question is below

Solved it by having my Notifier mailer extend Devise::Mailer and specifying Devise to use my Notifier as the mailer within config/initializers/devise.rb

snippet from config/initializers/devise.rb

# Configure the class responsible to send e-mails.
config.mailer = "Notifier"

My Notifier Mailer now

class Notifier < Devise::Mailer
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  # send a receipt of the Member's purchase
  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end

  # send password reset instructions
  def reset_password_instructions(user)
     @resource = user
     mail(:to => @resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
       format.html { render "devise/mailer/reset_password_instructions" }
     end
   end
end
Covey answered 15/4, 2011 at 16:13 Comment(1)
fyi, you can (and should) add your solution as an answer and then accept it.Codee
B
21

Using the latest version of Devise, the methods above didn't help me. This is my solution.

In config/application.rb:

config.action_mailer.delivery_method   = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }

In config/initializers/devise.rb:

config.mailer = "UserMailer" # UserMailer is my mailer class

In app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  default from: "[email protected]"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end

  # you can then put any of your own methods here
end

Finally, make sure you have generated custom devise views

rails generate devise:views

and move the email templates from app/views/devise/mailer/ to app/views/user_mailer/

mv app/views/devise/mailer/* app/views/user_mailer/
Backpack answered 7/9, 2011 at 17:55 Comment(2)
Needs to be updated to def reset_password_instructions(record,token,options)Negro
Just in case anyone finds this, the latest version of Devise's default mailer definitions have three arguments, not one. github.com/plataformatec/devise/blob/…Overweening
G
1

If you also want to specify 'tags' in postmark headers you have to do this in your mailer:

  # this override method is from Devise::Mailers::Helpers
  def headers_for(action)
    headers = {
      :subject        => translate(devise_mapping, action),
      :from           => mailer_sender(devise_mapping),
      :to             => resource.email,
      :template_path  => template_paths,
      :tag            => action.dasherize # specify the tag here
    }
    if resource.respond_to?(:headers_for)
      headers.merge!(resource.headers_for(action))
    end
    unless headers.key?(:reply_to)
      headers[:reply_to] = headers[:from]
    end
    headers
  end
Genip answered 2/12, 2011 at 22:56 Comment(1)
I had to change this line from :tag => action.dasherize to :tag => action.to_s.dasherize. Otherwise this seems to work perfectly.Microanalysis
M
0

I also had to generate the views for devise and copy the mail templates into the right place for my mailer. Something like this -

rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/
Mellman answered 16/5, 2011 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.