Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a custom email subject in a custom Devise email?

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?

like image 793
Omar Avatar asked Jun 08 '13 19:06

Omar


3 Answers

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'
like image 200
Kevin Avatar answered Oct 16 '22 06:10

Kevin


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'
like image 42
rails_id Avatar answered Oct 16 '22 07:10

rails_id


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'

like image 20
user2638707 Avatar answered Oct 16 '22 07:10

user2638707