Convert Figma logo to code with AI

premailer logopremailer

Preflight for HTML email

2,345
364
2,345
94

Top Related Projects

Email Blueprints is a collection of HTML email templates that can serve as a solid foundation and starting point for the design of emails

A few simple, but solid patterns for responsive HTML email templates and newsletters. Even in Outlook and Gmail.

A free simple responsive HTML email template

3,089

Juice inlines CSS stylesheets into your HTML source.

Rock-solid transactional email templates for applications.

17,026

MJML: the only framework that makes responsive-email easy

Quick Overview

Premailer is a Ruby library that inlines CSS styles into HTML markup, making it ideal for preparing HTML emails. It transforms external and internal CSS into inline styles, ensuring better compatibility across various email clients. Premailer also offers additional features like URL rewriting and CSS property stripping.

Pros

  • Improves email compatibility by inlining CSS styles
  • Supports both external and internal CSS stylesheets
  • Offers URL rewriting for better email rendering
  • Provides options for CSS property stripping and media query handling

Cons

  • May have performance issues with large HTML documents
  • Limited support for complex CSS selectors
  • Occasional inconsistencies in handling certain CSS properties
  • Requires additional setup for non-Ruby environments

Code Examples

  1. Basic usage with an HTML file:
require 'premailer'

premailer = Premailer.new('email.html', warn_level: Premailer::Warnings::SAFE)
output = premailer.to_inline_css
  1. Using Premailer with HTML string and custom options:
html = '<html><head><style>p { color: red; }</style></head><body><p>Hello, World!</p></body></html>'
premailer = Premailer.new(html, with_html_string: true, css_string: 'p { font-size: 16px; }')
output = premailer.to_inline_css
  1. Accessing warnings and processed CSS:
premailer = Premailer.new('email.html')
output = premailer.to_inline_css

puts premailer.warnings
puts premailer.processed_css

Getting Started

To use Premailer in your Ruby project:

  1. Install the gem:

    gem install premailer
    
  2. In your Ruby script:

    require 'premailer'
    
    premailer = Premailer.new('path/to/your/email.html', warn_level: Premailer::Warnings::SAFE)
    output = premailer.to_inline_css
    
    File.open('output.html', 'w') { |f| f.write(output) }
    

This will process your HTML file, inline the CSS, and save the result to output.html.

Competitor Comparisons

Email Blueprints is a collection of HTML email templates that can serve as a solid foundation and starting point for the design of emails

Pros of email-blueprints

  • Provides ready-to-use email templates for various purposes
  • Includes responsive designs optimized for different devices
  • Offers a comprehensive set of examples for different email types

Cons of email-blueprints

  • Requires manual implementation and customization
  • May not automatically handle CSS inlining or other email-specific optimizations
  • Limited to static templates without dynamic content generation

Code Comparison

email-blueprints:

<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td style="padding: 10px 0 30px 0;">
            <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border: 1px solid #cccccc; border-collapse: collapse;">
                <!-- Email content goes here -->
            </table>
        </td>
    </tr>
</table>

premailer:

require 'premailer'

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

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

The code snippets highlight the different approaches: email-blueprints provides HTML templates, while premailer focuses on processing and optimizing existing HTML for email compatibility.

A few simple, but solid patterns for responsive HTML email templates and newsletters. Even in Outlook and Gmail.

Pros of Cerberus

  • Focuses specifically on responsive email templates, providing a more specialized solution
  • Offers pre-built, tested templates that work across various email clients
  • Simpler to use for those who need ready-made email layouts

Cons of Cerberus

  • Less flexible for complex email styling needs
  • Limited to the provided templates and customization options
  • Doesn't offer inline CSS conversion or other advanced email processing features

Code Comparison

Premailer (Ruby):

require 'premailer'

premailer = Premailer.new(html, :warn_level => Premailer::Warnings::SAFE)
puts premailer.to_inline_css

Cerberus (HTML):

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td style="padding: 20px 0 30px 0;">
      <!-- Email content goes here -->
    </td>
  </tr>
</table>

Premailer is a more comprehensive tool for processing HTML emails, offering features like CSS inlining and link rewriting. It's better suited for complex email styling needs and automation.

Cerberus, on the other hand, provides pre-built responsive email templates that work across various clients. It's more suitable for those who need quick, ready-to-use email layouts without extensive customization.

The choice between the two depends on specific project requirements and the level of control needed over email styling and processing.

A free simple responsive HTML email template

Pros of responsive-html-email-template

  • Ready-to-use, responsive email template
  • Simple and clean design, easy to customize
  • Includes examples for various email types (welcome, alert, billing)

Cons of responsive-html-email-template

  • Limited to a single template style
  • Requires manual inline CSS for email client compatibility
  • Less flexibility compared to a full email processing library

Code Comparison

responsive-html-email-template:

<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" style="margin: auto;">
  <tr>
    <td style="padding: 20px 0; text-align: center">
      <h1 style="margin: 0;">Email Template</h1>
    </td>
  </tr>
</table>

premailer:

require 'premailer'

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

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

The responsive-html-email-template provides a ready-made HTML structure with inline styles, while premailer offers a programmatic approach to process and inline CSS for existing HTML emails. The choice between them depends on whether you need a quick template solution or a more flexible email processing tool.

3,089

Juice inlines CSS stylesheets into your HTML source.

Pros of juice

  • Faster processing speed, especially for large HTML files
  • More active development and maintenance
  • Better support for modern CSS properties and selectors

Cons of juice

  • Less comprehensive email client compatibility testing
  • Fewer options for customizing output and behavior
  • May not handle some complex CSS scenarios as well as Premailer

Code Comparison

premailer:

premailer = Premailer.new(html, with_html_string: true)
premailer.to_inline_css

juice:

const juice = require('juice');
const inlinedHtml = juice(html);

Both libraries aim to inline CSS for HTML email, but they differ in implementation and features. Premailer offers more extensive options for customization and email client compatibility, while juice focuses on speed and simplicity. Premailer is written in Ruby with ports to other languages, whereas juice is a native JavaScript library, making it more suitable for Node.js environments.

Premailer has been around longer and has a larger user base, which can be beneficial for finding solutions to common issues. However, juice's active development and focus on performance make it an attractive option for projects where speed is a priority.

When choosing between the two, consider your specific requirements, development environment, and the level of control you need over the inlining process.

Rock-solid transactional email templates for applications.

Pros of postmark-templates

  • Ready-to-use email templates for various scenarios
  • Responsive design for better compatibility across devices
  • Includes both HTML and plain text versions of templates

Cons of postmark-templates

  • Limited to specific use cases and may require customization
  • Lacks the ability to inline CSS automatically
  • No dynamic content generation capabilities

Code Comparison

postmark-templates:

<table class="email-content" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td class="email-body" width="100%" cellpadding="0" cellspacing="0">
      <table class="email-body_inner" align="center" width="570" cellpadding="0" cellspacing="0">
        <!-- Email Body Content -->
      </table>
    </td>
  </tr>
</table>

premailer:

require 'premailer'

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

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

The code snippets highlight the different approaches of the two projects. postmark-templates provides pre-built HTML structures for email templates, while premailer focuses on processing existing HTML and inlining CSS for improved email compatibility.

17,026

MJML: the only framework that makes responsive-email easy

Pros of MJML

  • Provides a high-level, component-based approach to email design
  • Offers a more intuitive and user-friendly syntax for creating responsive emails
  • Includes a live editor and preview tool for real-time development

Cons of MJML

  • Requires learning a new templating language specific to MJML
  • May have a steeper learning curve for developers unfamiliar with component-based systems

Code Comparison

MJML:

<mjml>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-text>Hello World</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

Premailer:

<html>
  <head>
    <style>
      p { color: red; }
    </style>
  </head>
  <body>
    <p>Hello World</p>
  </body>
</html>

Key Differences

  • MJML focuses on creating responsive emails from scratch using its own components
  • Premailer primarily inlines CSS and optimizes existing HTML for email clients
  • MJML generates HTML output, while Premailer modifies existing HTML
  • MJML has a steeper learning curve but offers more control over email design
  • Premailer is simpler to use with existing HTML templates but provides less design flexibility

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Premailer README CI 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 gem is a 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

gem install premailer

Example

require 'premailer'

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

# Write the plain-text output (must come before to_inline_css)
File.write "output.txt", premailer.to_plain_text

# Write the HTML output
File.write "output.html", premailer.to_inline_css

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

Adapters

  1. nokogiri (default)
  2. nokogiri_fast (20x speed, more memory)
  3. nokogumbo

(hpricot adapter removed, use ~>1.9.0 version if you need it)

Picking an adapter:

Premailer::Adapter.use = :nokogiri_fast

Ruby Compatibility

See .github/workflows/actions.yml for which ruby versions are tested. JRuby support is close, contributors are welcome.

Premailer-specific CSS

Premailer looks for a few CSS attributes that make working with tables a bit easier.

CSS AttributeAvailability
-premailer-widthAvailable on table, th and td elements
-premailer-heightAvailable on table, tr, th and td elements
-premailer-cellpaddingAvailable on table elements
-premailer-cellspacingAvailable on table elements
-premailer-alignAvailable 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'>

Plain text version

Premailer can generate a plain text version of your HTML. Links and images will be inlined.

For example

<a href="https://example.com" >
  <img src="https://github.com/premailer.png" alt="Premailer Logo" />
</a>

will become

Premailer Logo ( https://example.com )

To ignore/omit a section of HTML content from the plain text version, wrap it with the following comments.

<!-- start text/html -->
<p>This will be omitted from the plain text version.</p>
<p>
  This is extremely helpful for <strong>removing email headers and footers</strong>
  that aren't needed in the text version.
</p>
<!-- end text/html -->

Configuration options

For example:

Premailer.new(
  html, # html as string
  with_html_string: true,
  drop_unmergeable_css_rules: true
)

available options

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.

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.