11

I am using Devise in a Rails 3 application to create accounts. I have different types of users, so I want to send out custom password recovery emails based on the type of user.

I am able to send the custom email, I haven't found a way to set custom headers on that email. I am particularly interested in setting the subject of the email.

I have done the following:

  • Created a custom Devise mailer with a custom method inside. This method calls devise_mail with parameters. In this case, the custom mailer is called "reset_partner_instructions". I am able to call this mailer and successfully send an email from my User model.
  • Created a custom email view template which is successfully being called from devise_mail.

My custom mailer looks like this:

class AccountMailer < Devise::Mailer
  helper :application # gives access to all helpers defined within application_helper.
  def reset_partner_instructions(record, opts={})
    devise_mail(record, :reset_partner_instructions, opts)
  end
end

The problem is that the subject of the email is always "Reset partner instructions". I believe Devise is generating this title from the name of the mail template.

In this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer, they call the following code:

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

Since I'm calling "devise_mail" directly, I'm not seeing how to pass the headers intoto the mailer. Is there a simple setting or method I can use to set the email subject?

Omar
  • 243
  • 3
  • 10

5 Answers5

21

See devise helper

class AccountMailer < Devise::Mailer


   def confirmation_instructions(record, opts={})
    headers = {
        :subject => "Subject Here"
    }
    super
  end

end

Or you can change it in devise.en.yml file in intilizer directory

And set your own subject

mailer:
    confirmation_instructions:
        subject: 'Confirmation instructions'
rails_id
  • 8,120
  • 4
  • 46
  • 84
  • Thanks for your help! I ended up just creating a custom mailer. I felt it was a much cleaner solution, and I didn't have to override anything Devise was doing. – Omar Jun 18 '13 at 18:15
  • But how can I pass a variable to Locale? Like I want to use the first name in the subject. – Ammar Shah Feb 08 '18 at 11:43
  • @AmmarShah take a look [here](https://stackoverflow.com/a/13954066/1297435) – rails_id Feb 09 '18 at 01:32
6

It is very old question, it may be helpful still for you are others,

for custom subject:

  1. create a file config/locales/devise.en.yml
  2. add the content like this as shown below, make sure you do the indentation properly with 2 spaces as you do in database.yml file.

    en: devise: mailer: confirmation_instructions: subject: 'Verification subject here' reset_password_instructions: subject: 'Reset password subject here'

user2638707
  • 196
  • 2
  • 8
3

This is an old question but still comes up at the top of search and I was able to solve this by setting opts[:subject] instead of setting the header:

# https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer
class DeviseMailer < Devise::Mailer   
  helper :application
  include Devise::Controllers::UrlHelpers
  default template_path: 'devise/mailer'

  def confirmation_instructions(record, token, opts={})
    opts[:subject] = ...
    opts[:from] = ...
    opts[:reply_to] = ...
    super
  end
end

And in devise.rb:

  config.mailer = 'DeviseMailer'
Kevin
  • 266
  • 3
  • 16
0

I realized I should just use ActionMailer for this task. Devise wasn't giving me extra functionality, and since I was trying to generate a custom mailer, i could do that outside of Devise.

Omar
  • 243
  • 3
  • 10
-1

There's no need to set up a custom mailer or override Devise's confirmation_instructions method.

Devise let's you pass in optional parameters to the confirmation_instructions method that will get merged with Devise's default options (the opts hash gets passed through to Devise's headers_for helper).

So you could use the following code to send confirmation instructions with a custom subject:

Devise::Mailer.confirmation_instructions(user, {subject: "Custom Subject"}).deliver
  • When I use your solution, it does not work at all, When I set # template_path: "/app/views/shared/users/mailer", # template_name: "mytest.html.erb" , rails throws this error: Missing template /app/views/shared/users/mailer/mytest.html.erb with "mailer". Searched in: * "/app/views/shared/users/mailer". Although the file is there already. – datnt Feb 14 '15 at 04:25