40

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 define 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.?

4 Answers 4

65

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

6
  • 12
    Alternatively, you can add Devise::Mailer.layout "simple" to the top of your devise.rb initializer, before the setup block.
    – leppert
    Jul 7, 2013 at 17:15
  • Could you clarify what # email.haml or email.erb means? Did you mean to write # simple.haml or simple.erb ? Oct 28, 2013 at 19:37
  • Fixed. (The comment should be # simple.haml or simple.erb — it's been copied and pasted from the referenced Devise wiki but only the string "simple" was customised.)
    – djoll
    Mar 16, 2014 at 23:29
  • 2
    Use only Devise::Mailer.layout "simple" . config.to_prepare raises an error in later gem versions. Jul 20, 2014 at 9:25
  • 1
    This answer is still valid for Devise 3.5, though could use some more information. Note that the config.to_prepare needs to go in your application.rb, or you can use Rails.application.config.to_prepare in devise.rb. The link goes into a good amount of detail. Sep 22, 2016 at 15:28
45

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
1
  • 7
    This is the best answer.
    – DaCart
    May 28, 2019 at 7:58
20
# 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.

0
-3

Try reopen Devise::Mailer class:

 class Devise::Mailer < ActionMailer::Base
   layout 'sometemplate'
 end
1
  • didn't work. I also tried several other variations on the Class, but they didn't override the devise template, or non-template
    – holden
    Feb 17, 2011 at 20:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.