How to change "Devise: password reset instruction email's subject"
Asked Answered
M

7

32

I'm just unable to change "password reset instruction" email's subject. I have changed notifer.rb in Mailer to overwrite Devise default email subject. But it's not working.

Here in my application there is default Email subject inside Devise .yml file. But I want to make it dynamic to change it by pulling data from DB.

Monogamy answered 30/1, 2013 at 9:8 Comment(0)
H
64

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

And set your own subject for any mailer

                   mailer:
                     confirmation_instructions:
                       subject: 'Confirmation instructions'
                     reset_password_instructions:
                       subject: 'Reset password instructions'
                     unlock_instructions:
                       subject: 'Unlock Instructions'
Hildick answered 24/2, 2013 at 13:43 Comment(3)
config/locales directory?Denominational
config/locales/devise.en.ymlAuburn
You should have a path en.devise.mailer.confirmation_instructions.subject github.com/heartcombo/devise/blob/main/lib/devise/mailers/…Amylopsin
Z
19

I got this to work by creating my own sub-class of Devise::Mailer.

class DeviseMailer < Devise::Mailer
  def reset_password_instructions(record, token, opts={})
    mail = super
    # your custom logic
    mail.subject = "[SOME DB DATA]"
    mail
  end
end

And then modifying the devise.rb initializer to use my mailer.

# Configure the class responsible to send e-mails.
config.mailer = 'DeviseMailer'
Zionism answered 24/1, 2014 at 23:8 Comment(2)
as of today, the only example that works well. thanksBluepoint
Still the way to go in 2020Connotative
B
6

Change option :subject:

class DeviseMailer < Devise::Mailer
  def reset_password_instructions(record, token, opts={})
    opts[:subject] = 'SOME DB DATA'
    super
  end
end
Bun answered 24/4, 2018 at 20:28 Comment(1)
This is a great one answer.Trisyllable
A
1

You can write your own method inside your controller and call the respective mailer template. This will help you.. Else devise views, there will be a view page to send reset instruction. Change the content there..

Antenna answered 30/1, 2013 at 10:3 Comment(1)
I already tried to to pull data from DB to view page (inside devise), and it's not working, it's throwing an error "undefined method"Monogamy
M
1

For default foreign language (example Japanese)

STEP 1 Create a 'ja.yml' in config/locales/ (or whatever filename)

ja:
  devise:    
    mailer:
      confirmation_instructions:
        subject: '仮会員登録完了のお知らせ'
      reset_password_instructions:
        subject: 'パスワード再設定手順のお知らせ'

STEP 2 On config/environments/development.rb

config.i18n.default_locale = :ja

STEP 3 Restart server

Medeiros answered 5/12, 2016 at 6:14 Comment(0)
A
0

If you're willing to translate your Devise messages, which was my case, a better practice would be creating a new yml file in config/locale and changing your application's locale at config/application.rb

  1. To illustrate, I had to create devise.pt-BR.yml inside config/locale.

  2. Then I copied its translations from internet, on this link.

  3. Finally, I set my application's new locale at config/application.rb as follows:

    config.i18n.default_locale = :'pt-BR'
    

Hope it helps some of you guys having the same problem as mine.

Avery answered 2/11, 2014 at 19:56 Comment(0)
B
0

it worked for me to add the translation in devise.en.yml, but the right key for reset_password_instructions was user_subject instead of subject

  en:
    devise:
      mailer:
        reset_password_instructions:
          user_subject: Reset password instructions
Bangor answered 22/3, 2023 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.