How to use Haml with ActionMailer

Poor ActionMailer. It just doesn't get enough love.

If you try using Haml in your email templates, Rails will ignore your pretty little Haml code.

Why does this happen? Because, by default, ActionMailer only recognizes .erb or .rhtml extensions with templates that use a specific mime type (example: forgot_password.text.html.erb).

But thanks to changest 7534, ActionMailer can now use Haml too.

Just drop this into your environment.rb and restart:

ActionMailer::Base.register_template_extension('haml')

This should work on Rails 1.2.4 and up.

Extra credit

Don't use HTML email unless you really need it. You'll upset users and go crazy trying to make things work in a hundred email clients. Stick to text unless HTML can really add substantial value to your notifications.

November 15, 2007

New comments are disabled right now. Once we finish migrating this blog, we'll restore them.