简体   繁体   中英

Rails Devise custom mailers

I want to add new mailer methods to Devise that mostly copy the workflow of the built-in ones but with some added parameters and different views.

For example reset_password_instructions is used in two cases:

  1. When an existing user simply wants to reset his password.
  2. When a user invites another user, that user is saved in the database with a randomized password. He is sent an email with a reset_password_token that should redirect him to the edit password page so he could later sign in.

Same functionality, but I want the second email to have a different view, and also I need to be able to pass in the name of the person who sent the invitation.

It is very confusing because Devise mailers call a bunch of other methods and helpers so I don't know what methods to rewrite in order to achieve this.

To use a custom mailer, create a class that extends Devise::Mailer , like this:

class MyMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
end

Then, in your config/initializers/devise.rb , set config.mailer to "MyMailer" .

You may now use your MyMailer in the same way as any other mailer. In case you want to override specific mails to add extra headers, you can do so by simply overriding the method and calling super at the end of your custom method, to trigger Devise's default behavior.

For instance, we can add a new header for the confirmation_instructions e-mail as follows:

def confirmation_instructions(record, token, opts={})
  headers["Custom-header"] = "Bar"
  super
end

You can also override any of the basic headers (from, reply_to, etc) by manually setting the options hash:

def confirmation_instructions(record, token, opts={})
  headers["Custom-header"] = "Bar"
  opts[:from] = 'my_custom_from@domain.com'
  opts[:reply_to] = 'my_custom_from@domain.com'
  super
end

In order to get preview (if User is your devise model name):

# test/mailers/previews/my_mailer_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/my_mailer

class MyMailerPreview < ActionMailer::Preview

  def confirmation_instructions
    MyMailer.confirmation_instructions(User.first, "faketoken", {})
  end

  def reset_password_instructions
    MyMailer.reset_password_instructions(User.first, "faketoken", {})
  end

  def unlock_instructions
    MyMailer.unlock_instructions(User.first, "faketoken", {})
  end
end

In order to control which queue name the mailer queues up new email, add the following method to your class ( MyMailer , above):

def deliver_later
   Devise::Mailer.delay(queue: 'my_queue').send(...)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM