Omar
Omar

Reputation: 243

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:

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?

Upvotes: 11

Views: 7502

Answers (5)

Kevin
Kevin

Reputation: 244

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'

Upvotes: 3

user2638707
user2638707

Reputation: 306

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'

Upvotes: 7

Ross Feller
Ross Feller

Reputation: 64

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

Upvotes: -1

Omar
Omar

Reputation: 243

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.

Upvotes: 0

rails_id
rails_id

Reputation: 8220

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'

Upvotes: 23

Related Questions