premailer

Preflight for HTML email

Github星跟蹤圖

Premailer README Build Status Gem Version

What is this?

For the best HTML e-mail delivery results, CSS should be inline. This is a
huge pain and a simple newsletter becomes un-managable very quickly. This
script is my solution.

  • 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('')
  • CSS properties are checked against e-mail client capabilities
    • Based on the Email Standards Project's guides
  • A plain text version is created (optional)

Installation

Install the Premailer gem from RubyGems.

gem install premailer

or add it to your Gemfile and run bundle.

Example

require 'premailer'

premailer = Premailer.new('http://example.com/myfile.html', :warn_level => Premailer::Warnings::SAFE)

# Write the plain-text output
# This must come before to_inline_css (https://github.com/premailer/premailer/issues/201)
File.open("output.txt", "w") do, fout, fout.puts premailer.to_plain_text
end

# Write the HTML output
File.open("output.html", "w") do, fout, fout.puts premailer.to_inline_css
end

# Output any CSS warnings
premailer.warnings.each do, w, puts "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}"
end

Adapters

Premailer's default adapter is nokogiri if both nokogiri and nokogumbo are included in the Gemfile list. However, if you want to use a different adapter, you can choose to.

There are three adapters in total (as of premailer 1.10.0)

  1. nokogiri (default)
  2. nokogiri_fast
  3. nokogumbo

hpricot adapter removed due to its EOL, please use ~>1.9.0 version if You still need it..

NokogiriFast adapter improves the Algorithmic complexity of the running time by 20x with a slight compensation on memory. To switch to any of these adapters, add the following line. For example, if you want to include the NokogiriFast adapter,

Premailer::Adapter.use = :nokogiri_fast

Ruby Compatibility

Premailer is tested on Ruby 2.1 and above. JRuby support is close; contributors are welcome. Checkout the latest build status on the Travis CI dashboard.

Premailer-specific CSS

Premailer looks for a few CSS attributes that make working with tables a bit easier., CSS Attribute, Availability, -------------, ------------, -premailer-width, Available on table, th and td elements, -premailer-height, Available on table, tr, th and td elements, -premailer-cellpadding, Available on table elements, -premailer-cellspacing, Available on table elements, -premailer-align, Available on table elements, data-premailer="ignore", Available on link and style elements. Premailer will ignore these elements entirely., Each of these CSS declarations will be copied to appropriate element's attribute.

For example

table { -premailer-cellspacing: 5; -premailer-width: 500; }

will result in

<table cellspacing='5' width='500'>

Contributions

Contributions are most welcome. Premailer was rotting away in a private SVN repository for too long and could use some TLC. Fork and patch to your heart's content. Please don't increment the version numbers, though.

A few areas that are particularly in need of love:

  • Improved test coverage
  • Move un-repeated background images defined in CSS for Outlook

Credits and code

Thanks to all the wonderful contributors for their updates.

Thanks to Greenhood + Company for sponsoring some of the 1.5.6 updates,
and to Campaign Monitor for supporting the web interface.

The source code can be found on GitHub.

Copyright by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2007-2017. See LICENSE.md for license details.

主要指標

概覽
名稱與所有者premailer/premailer
主編程語言Ruby
編程語言Ruby (語言數: 3)
平台
許可證Other
所有者活动
創建於2009-11-26 05:31:46
推送於2025-04-01 16:27:23
最后一次提交2025-04-01 09:27:23
發布數52
最新版本名稱v1.27.0 (發布於 2024-08-26 09:26:25)
第一版名稱1.5.3 (發布於 2009-12-03 13:58:28)
用户参与
星數2.4k
關注者數57
派生數366
提交數797
已啟用問題?
問題數270
打開的問題數95
拉請求數148
打開的拉請求數2
關閉的拉請求數46
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?