Rails mailer templates now have the ability to utilize layouts just like their view-template siblings. The only caveat is that for a mailer layout to be automatically recognized it must end with _mailer. So this mailer:
1 2 3 4 5 6 |
class UserMailer < ActionMailer::Base def registration(user) subject "You've registered" from "system@example.com" end end |
would need a layout at layouts/user_mailer.html.erb. If you explicitly specify the layout it doesn’t need the _mailer suffix. So, in this example, a layout at layouts/email.html.erb would suffice:
1 2 3 4 |
class UserMailer < ActionMailer::Base layout 'email' ... end |
Go forth and make your emails pretty. You’ve got all the tools now…
tags: ruby, rubyonrails

Awesome! I’ve been doing this with a plugin I wrote, but I’m glad it’s finally added in. This is something that should have been added in a long time ago.
Yay!
Ridiculous. This feature is like two years overdue.
I was wondering why I couldn’t find a way to set the layout before. Now I know why. Of course, now I wonder why it didn’t exist before?
Usefull post! I will use it in my next project.
Yay! Very useful, I’m so glad they added this. No more hacking around to make it happen.
In other news, ActionMailer still sucks ;-)
I’m so glad to hear they’ve finally done this! Thanks for the post!
Hey Scott,
Why not help instead of just leaving comments ? Least you could do is make an actual wishlist of things you would like to see changed/implemented in AM. You never know, it might actually happen ;-)
I finally found the help I have been searching for. Thank you so much for writing this!!! But more than that, thanks a bunch for the help!!
Kendall
I’m getting
undefined method `layout’ for KMailer:Class /usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:385:in `method_missing’
tips?