Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Inline Styles Mailer Gem

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib Bump to 0.0.7 October 18, 2012
Octocat-spinner-32 spec Order mail parts to honour :parts_order option. October 18, 2012
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec Initial commit December 05, 2011
Octocat-spinner-32 .rvmrc Initial commit December 05, 2011
Octocat-spinner-32 .travis.yml Travis should only use Ruby 1.9.2 December 06, 2011
Octocat-spinner-32 CHANGELOG.md Smarter pickup of stylesheets and better testing to ensure we pick up… December 06, 2011
Octocat-spinner-32 Gemfile Initial commit December 05, 2011
Octocat-spinner-32 LICENSE Initial commit December 05, 2011
Octocat-spinner-32 README.md Tweak doc about Ruby 1.8.7 and Rails 3.0 and 2.3 December 06, 2011
Octocat-spinner-32 Rakefile Initial commit December 05, 2011
Octocat-spinner-32 inline_styles_mailer.gemspec Allow Rails 4.0 as a dependency March 30, 2013
README.md

Inline Styles Mailer Build Status

Using Jack Danger's excellent Inline Styles gem is even easier if you're using Rails 3.1 and this gem.

The Inline Styles gem helps you embed CSS styles into your markup so that you can send pretty HTML emails that won't get butchered by email clients that strip out CSS. Or, more precisely, will help reduce the amount of butchering (even with inline CSS some styles, like background images, are often cut out).

This gem stands on the shoulders of the inline_styles and sass-rails gem, merely adding some code to make it more convenient to use.

What do you mean, "inline style"?

Let's say you have a mail template like this:

<html>
  <body>
    <p>Hello World</p>
  </body>
</html>

And a CSS file like this:

p {
  color: red;
}

Then this gem will mash that all up into:

<html>
  <body>
    <p style="color: red;">Hello World</p>
  </body>
</html>

So you can keep your templates clean and take advantage of CSS preprocessing goodness if you want without compromising the portability of your HTML in various email clients.

MailChimp have a nice article: How To Code HTML Emails.

Installation

If you're using Bundler:

source 'http://rubygems.org'
gem 'inline_styles_mailer'

Usage

If you keep things simple, then it's just one line:

class FooMailer < ActionMailer::Base
  include InlineStylesMailer

  def foo(email)
    mail(:to => email, :subject => "Foo foo!")
  end

end

If you have a CSS file app/assets/stylesheets/_foo_mailer* (where * can be .css, .css.scss or .css.sass) then it will get automatically applied to the mail using the inline_styles gem. That name (_foo_mailer) is based on your mailer class name, e.g. FooMailer. If you have more than one file matching that pattern then it will use them all.

It will use one of three preprocessing methods based on the filename:

  • .scss
  • .sass
  • anything else (e.g. .css) no preprocessing at all

Want to use a different css file? Declare use_stylesheet:

class FooMailer < ActionMailer::Base
  include InlineStylesMailer
  use_stylesheet '_bar.css.sass'
  ...
end

You can use an array of stylesheets if you like. Don't keep your stylesheets in app/assets/stylesheets? Declare stylesheet_path:

class FooMailer < ActionMailer::Base
  include InlineStylesMailer
  stylesheet_path 'public/stylesheets'
  ...
end

Rails 3.0?

Maybe. This gem might work with Rails 3.0 (or Rails 3.1 without the asset pipeline enabled) but I haven't tested it. You'd have to use the stylesheet_path option for starters.

Rails 2.3?

Unlikely. Let me know if I'm wrong.

Ruby 1.8.7?

Not at the moment. I needlessly make use of some Ruby 1.9.2 syntax so this isn't going to work with Ruby 1.8.7. Patches welcome!

Development

Questions or problems? Please post them on the issue tracker. You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running bundle and rake.

The tests also run on Travis CI.

This gem was created by Bill Horsman and is under the MIT License.

Something went wrong with that request. Please try again.