Premailer は、各種メールクライアントで崩れず表示できるよう、CSS スタイルを HTML メール内にインライン展開するライブラリです。
カスタム属性の拡張
Premailer は、HTML テーブル属性を制御するための特殊な -premailer- CSS プロパティをサポートしています:
table, th, td {
/* table および cell タグの width 属性を設定 */
-premailer-width: 600px;
}
table, tr, th, td {
/* 要素の height 属性を設定 */
-premailer-height: 50px;
}
table {
/* table タグの HTML 属性を設定 */
-premailer-cellpadding: 0;
-premailer-cellspacing: 0;
-premailer-border: 0;
}
CLI での利用法
# 入力 HTML ファイルをインライン化して出力
premailer input.html > output.html
# リモートの HTML テンプレートを取得して変換
premailer https://example.com/newsletter.html > inlined.html
Ruby API
require 'premailer'
premailer = Premailer.new('email.html', warn_level: Premailer::Warnings::SAFE)
# インライン化された HTML を出力
File.open("output.html", "w") do |file|
file.puts premailer.to_inline_css
end
# プレーンテキスト版を出力
File.open("output.txt", "w") do |file|
file.puts premailer.to_plain_text
end