Seamlessly integrate Alex Dunae's premailer gem with ActionMailer.
Gmail doesn't support
<link> tags for HTML emails. Other webmail clients also
have problems with
This means that CSS must be inlined on each element, otherwise the email will not be displayed correctly in every client.
Inlining CSS is a pain to do by hand, and that's where the premailer gem comes in.
- CSS styles are converted to inline style attributes. Checks style and link[rel=stylesheet] tags, and preserves existing inline attributes.
- Relative paths are converted to absolute paths. Checks links in href, src and CSS url('')
This actionmailer_inline_css gem is a tiny integration between ActionMailer and premailer.
Installation & Usage
To use this in your Rails app, simply add
gem "actionmailer_inline_css" to your
- If you already have an HTML email template, all CSS will be automatically inlined.
- If you don't include a text email template, premailer will generate one from the HTML part. (Having said that, it is recommended that you write your text templates by hand.)
The current version of premailer doesn't support UTF-8, so I have written a small workaround to enforce it. This works for both Ruby 1.9 and 1.8.
Including CSS in Mail Templates
You can use the
stylesheet_link_tag helper to add stylesheets to your mailer layouts.
actionmailer_inline_css contains a premailer override that properly handles
these CSS URIs.
Add the following line to the
<head> section of app/views/layouts/build_mailer.html.erb:
<%= stylesheet_link_tag '/stylesheets/mailers/build_mailer' %>
This will add a stylesheet link for public/stylesheets/mailers/build_mailer.css. Premailer will then inline the CSS from that file, and remove the link tag.