The other day, I had to override devise confirmation instructions email.
subject of the email being sent out from
Devise makes it easy to override subject of the mail,
devise.en.yml file that stores
devise translations for different
mailer templates that are used.
devise.en.yml, it looks like the file given below.
I’ve trimmed down this YAML configuration just to have the context for this particular post.
Now, to override the subject, we can edit value of subject key, under confirmation_instructions key.
This is useful if we want to change the subject and do not want to add dynamic information in the subject. Let’s say, we want to add name of the person logged in, this is not possible with variable interpolation in strings of YAML files.
This is not possible, as we don’t have a control over when the subject interpolation is called before sending our an email from Devise.
We can override
Devise::Mailer and customize behavior.
Listing down some of the common needs where we would want to implement Custom Mailer
- Override Subject with dynamic interpolation of the emails (confirmation_instructions, password_change, ..etc)
- Pass some extra header information in the emails being sent out in email
Override Subject in Devise Mailer
1. Create devise_mailer.rb file
Create a new mailer, that override default
Devise::Mailer as given below.
Here, we have created a new Custom Mailer with name
that inherits from
overriden behavior for
mail sent from devise.
The arguments received:
record: User record for which confirmation instructions are being sent out
token: User token information
opts: Hash with any extra options
2. Instruct Devise to use Custom Mailer
Now, that we have created a custom mailer
we need to register this mailer with Devise.
Devise already provides a way, to register such custom mailer.
As we can see, devise by default uses
We need to change this line with a line given below.
Now, devise will override subject as given in
Override header in Devise Mailer
Similarly, we can override header information as given below.
to for the email being sent out.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox