https://github.com/fphilipe/premailer-rails
CSS styled emails without the hassle.
https://github.com/fphilipe/premailer-rails
Keywords
css email gem inline-css rails ruby
Keywords from Contributors
activerecord activejob mvc rubygems background-jobs rubocop crash-reporting feature-flag ruby-gem nokogiri
Last synced: about 4 hours ago
JSON representation
Repository metadata
CSS styled emails without the hassle.
- Host: GitHub
- URL: https://github.com/fphilipe/premailer-rails
- Owner: fphilipe
- License: mit
- Created: 2011-04-03T00:05:17.000Z (almost 15 years ago)
- Default Branch: main
- Last Pushed: 2024-06-17T05:56:17.000Z (over 1 year ago)
- Last Synced: 2026-02-22T13:57:47.750Z (9 days ago)
- Topics: css, email, gem, inline-css, rails, ruby
- Language: Ruby
- Homepage:
- Size: 331 KB
- Stars: 1,749
- Watchers: 8
- Forks: 254
- Open Issues: 12
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
README.md
premailer-rails
CSS styled emails without the hassle.
Introduction
This gem is a drop in solution for styling HTML emails with CSS without having
to do the hard work yourself.
Styling emails is not just a matter of linking to a stylesheet. Most clients,
especially web clients, ignore linked stylesheets or <style> tags in the HTML.
The workaround is to write all the CSS rules in the style attribute of each
tag inside your email. This is a rather tedious and hard to maintain approach.
Premailer to the rescue! The great premailer gem applies all CSS rules to each
matching HTML element by adding them to the style attribute. This allows you
to keep HTML and CSS in separate files, just as you're used to from web
development, thus keeping your sanity.
This gem is an adapter for premailer to work with actionmailer out of the box.
Actionmailer is the email framework used in Rails, which also works outside of
Rails. Although premailer-rails has certain Rails specific features, it also
works in the absence of Rails making it compatible with other frameworks such
as sinatra.
How It Works
premailer-rails works with actionmailer by registering a delivery hook. This
causes all emails that are delivered to be processed by premailer-rails. This
means that by simply including premailer-rails in your Gemfile you'll get
styled emails without having to set anything up.
Whenever premailer-rails processes an email, it collects the URLs of all linked
stylesheets (<link rel="stylesheet" href="css_url">). Then, for each of these
URLs, it tries to get the content through a couple of strategies. As long as
a strategy does not return anything, the next one is used. The strategies
available are:
-
:filesystem: If there's a file insidepublic/with the same path as in
the URL, it is read from disk. E.g. if the URL is
http://cdn.example.com/assets/email.cssthe contents of the file located
atpublic/assets/email.cssgets returned if it exists. -
:asset_pipeline: If Rails is available and the asset pipeline is enabled,
the file is retrieved through the asset pipeline. E.g. if the URL is
http://cdn.example.com/assets/email-fingerprint123.css, the file
email.cssis requested from the asset pipeline. That is, the fingerprint
and the prefix (in this caseassetsis the prefix) are stripped before
requesting it from the asset pipeline. -
:network: As a last resort, the URL is simply requested and the response
body is used. This is useful when the assets are not bundled in the
application and only available on a CDN. On Heroku e.g. you can add assets
to your.slugignorecausing your assets to not be available to the app
(and thus resulting in a smaller app) and deploy the assets to a CDN such
as S3/CloudFront.
You can configure which strategies you want to use as well as specify their
order. Refer to the Configuration section for more on this.
Note that the retrieved CSS is cached when the gem is running with Rails in
production.
Installation
Simply add the gem to your Gemfile:
gem 'premailer-rails'
premailer-rails and premailer require a gem that is used to parse the email's
HTML. For a list of supported gems and how to select which one to use, please
refer to the Adapter
section of premailer. Note
that there is no hard dependency from either gem so you should add one yourself.
Also note that this gem is only tested with nokogiri.
Configuration
Premailer itself accepts a number of options. In order for premailer-rails to
pass these options on to the underlying premailer instance, specify them
as follows (in Rails you could do that in an initializer such as
config/initializers/premailer_rails.rb):
Premailer::Rails.config.merge!(preserve_styles: true, remove_ids: true)
For a list of options, refer to the premailer documentation. The default
configs are:
{
input_encoding: 'UTF-8',
generate_text_part: true,
strategies: [:filesystem, :asset_pipeline, :network]
}
If you don't want to automatically generate a text part from the html part, set
the config :generate_text_part to false.
Note that the options :with_html_string and :css_string are used internally
by premailer-rails and thus will be overridden.
If you're using this gem outside of Rails, you'll need to call
Premailer::Rails.register_interceptors manually in order for it to work. This
is done ideally in some kind of initializer, depending on the framework you're
using.
premailer-rails reads all stylesheet <link> tags, inlines the linked CSS
and removes the tags. If you wish to ignore a certain tag, e.g. one that links to
external fonts such as Google Fonts, you can add a data-premailer="ignore"
attribute.
Usage
premailer-rails processes all outgoing emails by default. If you wish to skip
premailer for a certain email, simply set the :skip_premailer header:
class UserMailer < ActionMailer::Base
def welcome_email(user)
mail to: user.email,
subject: 'Welcome to My Awesome Site',
skip_premailer: true
end
end
Note that the mere presence of this header causes premailer to be skipped, i.e.,
even setting skip_premailer: false will cause premailer to be skipped. The
reason for that is that the skip_premailer is a simple header and the value is
transformed into a string, causing 'false' to become truthy.
Emails are only processed upon delivery, i.e. when calling #deliver on the
email, or when previewing them in
rails.
If you wish to manually trigger the inlining, you can do so by calling the hook:
mail = SomeMailer.some_message(args)
Premailer::Rails::Hook.perform(mail)
This will modify the email in place, useful e.g. in tests.
Supported Rails Versions
This gem is tested on Rails versions 5 through 7.
For Rails 7, it support both the classical Sprockets asset pipeline as well as the new Propshaft gem.
If you're looking to integrate with Webpacker, check out these instructions.
Small Print
Author
Philipe Fatio (@fphilipe)
License
premailer-rails is released under the MIT license. See the license file.
Owner metadata
- Name: Philipe Fatio
- Login: fphilipe
- Email:
- Kind: user
- Description:
- Website: phili.pe
- Location: Zurich, Switzerland
- Twitter: fphilipe
- Company: @valora-digital
- Icon url: https://avatars.githubusercontent.com/u/128151?v=4
- Repositories: 60
- Last ynced at: 2023-04-10T01:34:01.461Z
- Profile URL: https://github.com/fphilipe
GitHub Events
Total
- Pull request event: 1
- Fork event: 3
- Issues event: 3
- Watch event: 48
- Issue comment event: 1
Last Year
- Fork event: 2
- Issues event: 1
- Watch event: 26
Committers metadata
Last synced: 2 days ago
Total Commits: 335
Total Committers: 57
Avg Commits per committer: 5.877
Development Distribution Score (DDS): 0.227
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Philipe Fatio | me@p****e | 259 |
| Kir Shatrov | s****v@m****m | 7 |
| Javier Julio | j****l@g****m | 7 |
| Rolly Fordham | r****y@l****z | 5 |
| wonda-tea-coffee | l****t@g****m | 2 |
| Rafael Masson | r****n@g****m | 2 |
| Jonathan Goldman | me@j****m | 2 |
| Florian Dütsch | m****l@f****e | 2 |
| Andrew Hubbs | a****w@h****s | 1 |
| Atinder Singh | a****a@g****m | 1 |
| Bitdeli Chef | c****f@b****m | 1 |
| Brandon Hilkert | b****t@g****m | 1 |
| Christian Schmidt | g****b@c****k | 1 |
| Dmytrii Nagirniak | d****r@g****m | 1 |
| Earle Clubb | e****b@v****m | 1 |
| Sven Winkler | s****r@n****e | 1 |
| Steve Dee | s****e@c****m | 1 |
| Sijawusz Pur Rahnama | s****a@p****m | 1 |
| Said Kaldybaev | s****v@m****m | 1 |
| Roman Marynchak | r****k@g****m | 1 |
| Pete Hopkins | p****s@t****m | 1 |
| Michael Reinsch | m****l@m****m | 1 |
| Mauricio Menegaz | m****z@t****r | 1 |
| Mark Edmondson | m****k@g****m | 1 |
| Marius Bobin | m****n@g****m | 1 |
| Greg Woodward & Justin Richard | p****n@p****m | 1 |
| Jonathan del Strother | j****r@a****m | 1 |
| Kai Schlichting | l****t@g****e | 1 |
| zdenal | n****z@g****m | 1 |
| jlxw | j****n@j****m | 1 |
| and 27 more... | ||
Committer domains:
- me.com: 2
- phili.pe: 1
- luma.co.nz: 1
- jonnii.com: 1
- florian-duetsch.de: 1
- hub.bs: 1
- bitdeli.com: 1
- chsc.dk: 1
- valcom.com: 1
- nix-wie-weg.de: 1
- causes.com: 1
- positionly.com: 1
- malwarebytes.com: 1
- twitter.com: 1
- mobalean.com: 1
- tre-rs.gov.br: 1
- guestfolio.com: 1
- gitlab.com: 1
- pivotallabs.com: 1
- audioboo.fm: 1
- gmx.de: 1
- jeyel.com: 1
- atwam.com: 1
- radicaos.com: 1
- mobilefringe.com: 1
- 4angle.com: 1
- iki.fi: 1
- gen-x-design.com: 1
- igas.me: 1
- siami.fr: 1
- alethearose.com: 1
- instructure.com: 1
Issue and Pull Request metadata
Last synced: 11 days ago
Total issues: 55
Total pull requests: 55
Average time to close issues: 3 months
Average time to close pull requests: 3 months
Total issue authors: 51
Total pull request authors: 43
Average comments per issue: 3.84
Average comments per pull request: 1.91
Merged pull request: 25
Bot issues: 0
Bot pull requests: 0
Past year issues: 2
Past year pull requests: 0
Past year average time to close issues: about 17 hours
Past year average time to close pull requests: N/A
Past year issue authors: 2
Past year pull request authors: 0
Past year average comments per issue: 0.5
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- ghost (3)
- robsdudeson (2)
- jmuheim (2)
- curtp (1)
- derekwheel (1)
- eric1234 (1)
- davidwessman (1)
- Saidbek (1)
- dlouzan (1)
- n-rodriguez (1)
- aguynamedben (1)
- javierjulio (1)
- irene-chi19 (1)
- kimardenmiller (1)
- hadifarnoud (1)
Top Pull Request Authors
- kirs (7)
- fphilipe (4)
- javierjulio (3)
- wonda-tea-coffee (3)
- stanhu (2)
- ywen (1)
- sebjacobs (1)
- Saidbek (1)
- AnrichVS (1)
- tilthouse (1)
- apauly (1)
- maschwenk (1)
- ThriceGood (1)
- caseyprovost (1)
- mbobin (1)
Top Issue Labels
- feature request (1)
Top Pull Request Labels
Package metadata
- Total packages: 13
-
Total downloads:
- rubygems: 205,244,309 total
- Total docker downloads: 903,060,032
- Total dependent packages: 17 (may contain duplicates)
- Total dependent repositories: 4,637 (may contain duplicates)
- Total versions: 110
- Total maintainers: 1
gem.coop: premailer-rails
This gem brings you the power of the premailer gem to Rails without any configuration needs. Create HTML emails, include a CSS file as you do in a normal HTML document and premailer will inline the included CSS.
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: http://www.rubydoc.info/gems/premailer-rails/
- Licenses: MIT
- Latest release: 1.12.0 (published over 3 years ago)
- Last Synced: 2026-03-01T21:01:30.388Z (2 days ago)
- Versions: 25
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 102,374,661 Total
- Docker Downloads: 451,530,016
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.123%
- Docker downloads count: 0.244%
- Downloads: 0.247%
- Maintainers (1)
rubygems.org: premailer-rails
This gem brings you the power of the premailer gem to Rails without any configuration needs. Create HTML emails, include a CSS file as you do in a normal HTML document and premailer will inline the included CSS.
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: http://www.rubydoc.info/gems/premailer-rails/
- Licenses: MIT
- Latest release: 1.12.0 (published over 3 years ago)
- Last Synced: 2026-03-02T11:02:09.726Z (1 day ago)
- Versions: 25
- Dependent Packages: 15
- Dependent Repositories: 4,603
- Downloads: 102,391,260 Total
- Docker Downloads: 451,530,016
-
Rankings:
- Docker downloads count: 0.303%
- Downloads: 0.317%
- Dependent repos count: 0.464%
- Average: 0.885%
- Stargazers count: 1.248%
- Dependent packages count: 1.364%
- Forks count: 1.612%
- Maintainers (1)
gem.coop: premailer-rails3
This gem brings you the power of the premailer gem to Rails without any configuration needs. Create HTML emails, include a CSS file as you do in a normal HTML document and premailer will inline the included CSS.
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: http://www.rubydoc.info/gems/premailer-rails3/
- Licenses: mit
- Latest release: 1.3.2 (published almost 13 years ago)
- Last Synced: 2026-03-02T09:33:21.225Z (1 day ago)
- Versions: 10
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 239,196 Total
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 1.958%
- Downloads: 5.875%
- Maintainers (1)
rubygems.org: premailer-rails3
This gem brings you the power of the premailer gem to Rails without any configuration needs. Create HTML emails, include a CSS file as you do in a normal HTML document and premailer will inline the included CSS.
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: http://www.rubydoc.info/gems/premailer-rails3/
- Licenses: mit
- Latest release: 1.3.2 (published almost 13 years ago)
- Last Synced: 2026-02-28T13:01:41.593Z (3 days ago)
- Versions: 10
- Dependent Packages: 2
- Dependent Repositories: 34
- Downloads: 239,192 Total
-
Rankings:
- Stargazers count: 1.245%
- Forks count: 1.603%
- Average: 3.503%
- Dependent repos count: 4.253%
- Downloads: 5.146%
- Dependent packages count: 5.267%
- Maintainers (1)
proxy.golang.org: github.com/fphilipe/premailer-rails
- Homepage:
- Documentation: https://pkg.go.dev/github.com/fphilipe/premailer-rails#section-documentation
- Licenses: mit
- Latest release: v1.12.0 (published over 3 years ago)
- Last Synced: 2026-02-28T13:01:40.958Z (3 days ago)
- Versions: 32
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 6.463%
- Average: 6.681%
- Dependent repos count: 6.899%
ubuntu-23.10: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Licenses:
- Latest release: 1.10.3-4 (published 18 days ago)
- Last Synced: 2026-02-13T18:29:52.891Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-11: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: https://packages.debian.org/bullseye/ruby-premailer-rails
- Licenses:
- Latest release: 1.10.3-3 (published 21 days ago)
- Last Synced: 2026-02-13T08:23:29.015Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-20.04: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Licenses:
- Latest release: 1.10.3-2 (published 18 days ago)
- Last Synced: 2026-02-13T07:20:12.807Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-22.04: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Licenses:
- Latest release: 1.10.3-3 (published 18 days ago)
- Last Synced: 2026-02-13T13:23:10.442Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-23.04: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Licenses:
- Latest release: 1.10.3-3 (published 20 days ago)
- Last Synced: 2026-02-11T06:46:58.703Z (20 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-12: ruby-premailer-rails
- Homepage: https://github.com/fphilipe/premailer-rails
- Documentation: https://packages.debian.org/bookworm/ruby-premailer-rails
- Licenses:
- Latest release: 1.10.3-4~deb12u1 (published 19 days ago)
- Last Synced: 2026-02-12T23:37:37.666Z (19 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
Dependencies
- arel >= 0
- byebug >= 0
- racc >= 0
- rails >= 0
- rubysl >= 0
- sprockets-rails >= 0
- nokogiri >= 0 development
- rspec ~> 3.3 development
- actionmailer >= 3
- premailer ~> 1.7, >= 1.7.9
- actions/checkout v3 composite
- ruby/setup-ruby v1 composite
- premailer-rails >= 0
- rails ~> 5.2, >= 5.2.8.1
- premailer-rails >= 0
- rails ~> 6.1.7
- premailer-rails >= 0
- propshaft >= 0
- rails ~> 7.0.4
- premailer-rails >= 0
- rails ~> 7.0.4
- sprockets-rails >= 0
Score: 32.34279006224437