Premailer converts stylesheet and style-tag CSS into inline HTML attributes for email clients. It can absolutize URLs, check client capabilities, and optionally build a plain-text version.
Overview
Premailer is a Ruby library for preparing HTML email before send. Many clients render messages more reliably when CSS is inlined on elements; Premailer reads style and link rel=stylesheet blocks, merges them with existing inline styles, and writes HTML suited to common mail clients. Guidance follows Email Standards Project compatibility notes.
The gem resolves relative href, src, and CSS url() values to absolute URLs when a base is provided. It warns when properties may not render in specific clients. Optional plaintext output strips head and style content and inlines link text for images. Sections can be omitted from plaintext via HTML comment wrappers.
Premailer-specific CSS attributes such as -premailer-width and -premailer-cellpadding map table layout hints onto HTML attributes. Remote stylesheets with integrity attributes are verified; cssparser blocks fetching CSS from private hosts unless explicitly allowed for trusted HTML. Adapters include nokogiri by default and optional faster parsers.
Developers instantiate Premailer with HTML input, call to_inline_css for HTML and to_plain_text when needed, and inspect warnings. It installs from RubyGems or Bundler and is suited to Rails or standalone Ruby mail pipelines, with documented PostCSS steps for resolving CSS variables in email stylesheets.
Key features
- CSS inlined into HTML attributes
- style and stylesheet link processing
- Relative to absolute URL conversion
- Email client capability warnings
- Optional plain-text generation
- Premailer-specific table CSS hooks
- Subresource integrity on remote CSS
- Pluggable HTML parser adapters
Use cases
- Newsletter HTML preprocessing
- Rails mailer inline CSS step
- Plain-text fallback for campaigns
- Table-heavy email layout conversion
- Validating CSS against client support