Inlining CSS in ActionMailer 3 (Rails 3.0) July 8th, 2010

As most of you already know, a bunch of online mail readers (like Hotmail, Gmail, …) don’t handle stylesheets very well, they prefer inline styling (

TamTam is a gem created by Dave Hoover and Brian Tatnall that does all the dirty work for us. Before Rails 3 there was a plugin called inline_css (by the same authors) that used TamTam to automatically inlined all mails sent by ActionMailer. Since Rails 3, however, the ActionMailer internals have changed, and the plugin stopped working.

For my own use I created a module called InlineCss that I include in the mailers I need inlining for. This is the code:

module InlineCss
  def render(*args)
    if (template = args.first[:template]) && template.mime_type.html?
      # Be warned, this code expects a <style> tag in the head of your layout.
      TamTam.inline(:document => super)
    else
      super
    end
  end
end

This code sees if I am working with a template and if that template has a HTML MIME-type, it inline’s it with TamTam.

In my mailers I just include it. If you want to have this in all mailers, just add an initializer that includes InlineCss in ActionMailer::Base, and it should work

tags: , , , l

40 Responses to “Inlining CSS in ActionMailer 3 (Rails 3.0)”

  • about 1 year ago localtime said

    Very well written. The script is very useful and helpful, many thanks!

  • 11 months ago Go ped said

    Very well written. The script is very useful and helpful, many thanks!

  • 8 months ago cazare in predeal said

    The css file editing can be done with DreamWeaver or with notepad, too.

  • 8 months ago optimizare seo said

    I think the idea of css is to reduce the space necessary for the browser.

  • 6 months ago Best Sunglasses said

    Reading is my passion. Browsing through your site gives me a lot of knowledge in so many ways. Best Sunglasses

  • 6 months ago Designer womens clothing said

    There are different ways in which you might want to set up a complete development environment for Ruby on Rails on a Windows machine. I like to discuss more about this thanks for sharing. Regards, Jack

  • 6 months ago thailand said

    Thank you for this information. I’ve been looking for something like this for quite a while. Keep up the good work, cheers! klimat thailand lången tips filippinerna

  • 6 months ago sdfds said

    Very well written. The script is very useful and helpful, many thanks!what does it mean when you dream about someone fh what does it mean when your eye twitches

  • 5 months ago moulin a vendre said

    I will get to know more new information. Even the website layouts and the designs impress me a lot. Minoterie pour farine

  • 5 months ago best desktops said

    The concepts have however been extended from their computing and telecommunication meanings into the area of human interaction and conversation, such that even offline can be used in contrast to the common usage of online. Thanks.

  • 5 months ago bilverkstad växjö said

    Thanks for a good post. I think the idea of css to recude space is necessary.

  • 5 months ago synas pÃ¥ google said

    Thanks for a good post. Its great that you are posting stuff like this.

  • 5 months ago synas pa google said

    Great post! Keep them coming like this. There is too much bad blogs out there, but this is one of the good ones.

  • 5 months ago Aluminum Wallet said

    I want to share this information with my friends I hope the will also like it.

  • 4 months ago Peanut Machine said

    So this is what happened with me, anyways its a confirming toil, I revalue it. Thanks

  • 4 months ago onthebroadway said

    A typical event features several vignettes of the daily everyday life of a pair with the camera mostly centered upon them, often cropping out other font, giving it its unique visual style.

  • 4 months ago Exotic countertops said

    In my mailers I just include it. If you want to have this in all mailers, just add an initializer that includes InlineCss in ActionMailer::Base, and it should work

  • 4 months ago granite Houston TX said

    Local per-repo rules can be added to the .git/info/exclude file in your repo. These rules are not committed with the repo so they are not shared with others. This method can be used for locally-generated files that you don’t expect other users to generate, like files created by your editor.

  • 4 months ago Dried Fruits said

    ..qui eussent aim¨¦ prendre l’air? Le si¨¨cle dernier aurait-on imagin¨¦ nommer L¨¦on Zitrone ministre des sports?

  • 4 months ago Peanut Machines For Sale said

    Observateur, la qualifiant d¡¯infâme¡­ Notre Ministre de la Culture visiblement ne supporte pas la critique.

  • 3 months ago Josh said

    Was pleased reading about this stuff. Thanks! buy college essays

  • 3 months ago onthebroadway said

    In Ireland religion is taught in a subject called holy Education which is necessary in many schools for the Junior record, but available as an option for the Leaving diploma.

  • 3 months ago aluma wallet said

    Thank you for sharing your blog. I came across it just recently and I am very impressed. Keep it going.

  • about 1 month ago Printing said

    Can I just say, this blog is what got me through the day today. Every time I read it, I just get more and more excited about whats next. Very refreshing blog and very refreshing ideas. Im glad that I came across this when I did. I love what youve got to say and the way you say it. Printing

  • about 1 month ago Erik said

    Våffeljärn

  • about 1 month ago Azithromycin uses said

    These rules are not committed with the repo so they are not shared with others. This method can be used for locally-generated files that you don’t expect other users to generate, like files created by your editor.Azithromycin uses

  • about 1 month ago Erik said

    Bygga garage

  • about 1 month ago Erik said

    Byta fönster

  • 25 days ago optimizare seo said

    I think the idea of css is to reduce the space necessary for the browser.

  • 16 days ago fashion said

    Images fashion strives to provide a balance of timely, credible business news and key fashion trends. fashion apparel news

  • 16 days ago caplan syndrome said

    The concepts have however been extended from their computing and caplan syndrome telecommunication meanings into the area of human interaction and conversation, such that even offline can be used in contrast to the common usage of online.

  • 15 days ago HENRY said

    I am really looking forward to skiing there. It is so very pretty and amazing there. Hopefully I can visit there one day. Residential plots in gurgaon

  • 12 days ago warcraft cheats said

    My spouse and i esteem the particular helpful data a person supply with your content. I most certainly will search for your site in addition to click here frequently.

  • 10 days ago IT outsourcing services UK said

    good work keep it up

  • 6 days ago music production said

    It is a wonderful blog. It helps me out a lot. Thank you. I really need help in development and blogs like yours are very helpful.

  • 4 days ago me not you said

    Keep it creative, didnt you all know thats the key to make it in this world.

  • 4 days ago webbplasts said

    Happy like a pancake! Shake shake, this made my day.

  • 1 day ago erp manufacturing software said

    It is a wonderful blog. It helps me out a lot. Thank you. I really need help in development and blogs like yours are very helpful.

  • about 9 hours ago Legal Supplements said

    I’m definitely tired of struggling to find relevant and intelligent comments on this topic. Everybody today seems to go to extremes to either drive home their views or suggest that anyone else in the world is wrong. Thank you for your accurate and relevant insights.

  • about 9 hours ago strongest legal steroids said

    Please tell me that youre going to keep this up! Its so good and so important. I cant wait to read more from you.
    I just feel like you know so much and know how to make people listen to what you have to say. This blog is just too cool to be missed. Great stuff, really. Please, PLEASE keep it up!

Leave a Reply