Override Devise Mailer in Rails
The other day, I had to override devise confirmation instructions email.
Particularly, subject
of the email being sent out from
Devise.
Devise makes it easy to override subject of the mail,
with devise.en.yml
file that stores
devise translations for different
mailer templates that are used.
Devise provides 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.
Override Devise::Mailer
We can override Devise::Mailer
and customize behavior.
Listing down some of the common needs where we would want to implement Custom Mailer
from Devise::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 DeviseMailer
,
that inherits from Devise::Mailer
and
overriden behavior for confirmation_instructions
mail sent from devise.
The arguments received:
record
: User record for which confirmation instructions are being sent outtoken
: User token informationopts
: Hash with any extra options
2. Instruct Devise to use Custom Mailer
Now, that we have created a custom mailer DeviseMailer
,
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 Devise::Mailer
class.
We need to change this line with a line given below.
Now, devise will override subject as given in devise_mailer.rb
file.
Override header in Devise Mailer
Similarly, we can override header information as given below.
This override, from
and to
for the email being sent out.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox