Skip to content

How To: Use custom mailer

ahey edited this page Nov 9, 2012 · 27 revisions

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`.
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 after (triggering Devise's default behavior). For instance, we can add a new header for the confirmation_instructions e-mail as follow:

You can still use devise views. You basically need to override the methods you mentioned and call super after:

def confirmation_instructions(record)
  headers["Custom-header"] = "Bar"
  super
end

Remember, it is simply Ruby and Rails after all. Enjoy!

Note that you will need to copy the devise mail views from app/views/devise/mailer to your new mailer folder. For example, with the MyMailer mailer above, you need to copy the devise mail views to app/views/my_mailer/. You need to copy over all of the views even if you're only customising one. For example if you are customising confirmation_instructions.html.erb you also need to copy over reset_password_instructions.html.erb and unlock_instructions.html.erb, or those emails will silently start being sent as blank emails.

Clone this wiki locally