Premailex - Inline email styling made simple

I needed something like premailer for rails, and after searching, I decided to make an easy-to-use library to handle inline styles and convert to text for emails. It’s straightforward, and it handles external stylesheets too!

Here’s an example with Bamboo using a Phoenix template:

def welcome_email do
  new_email
  |> subject("Email subject")
  |> to("test@example.com")
  |> from("test@example.com")
  |> put_text_layout(false)
  |> render("email.html")
  |> premail()
end

defp premail(email) do
  html = Premailex.to_inline_css(email.html_body)
  text = Premailex.to_text(email.html_body)

  email
  |> html_body(html)
  |> text_body(text)
end

I hope some of you’ll find this library useful!

14 Likes

Much appreciated.