How do I configure devise to use a custom email layout?
When I say layout I don't mean just simply the views, I generate those. On all my own mailers I'm using a default layout. Which I defin开发者_JS百科e in the SomeMailer.rb file
#some_mailer.rb
class SomeMailer < ActionMailer::Base
layout 'sometemplate'
Is there some way I can do this for the Devise Mailer et al.?
Found the answer sitting in the Devise Github wiki,
Reading that helps. ;-)
config.to_prepare do
Devise::Mailer.layout "simple" # simple.haml or simple.erb
Devise::Mailer.helper :mailer
end
Here is the reference of the wiki page: How To: Create custom layouts
There is also a parent_mailer option in devise.rb, let's say you are sending emails outside of devise, by default this option is set to ActionMailer::Base, but if you have an ApplicationMailer that already is inheriting from ActionMailer::Base, you could change parent_mailer to this and get all your layouts and configurations out of the box.
In any case is a lot cleaner to use this to keep the rails flow of layouts in your applications if you don't want to change anything in the devise mailer controller.
# devise.rb
config.parent_mailer = 'ApplicationMailer'
# application_mailer.rb
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
end
# Devise::Mailer inherits from ActionMailer::Base other mail will work fine.
## app/mailers/deviser_mailer.rb
class DeviseMailer < Devise::Mailer
layout 'email'
default from: I18n.t("mailer.default.from")
end
## then in config/initializer/devise.rb
# Configure the class responsible to send e-mails.
config.mailer = "DeviseMailer"
Make sure to restart your rails server as you changed an initializer.
Try reopen Devise::Mailer class:
class Devise::Mailer < ActionMailer::Base
layout 'sometemplate'
end
精彩评论