danschultzer

danschultzer

Pow Core Team

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!

https://github.com/danschultzer/premailex

Showing Posts 1 to 4

ragamuf

ragamuf

Much appreciated.

danschultzer

danschultzer OP

Pow Core Team

Premailex v1.0!

Reached a huge milestone that finally got Premailex to a place I wanted it to be since first release, mainly:

  1. CSS parsing is done with binary parsing rather than regular expression
  2. Zero dependencies with a naive :xmerl fallback for HTML parsing if none of LazyHTML, Floki, Meeseeks are available

A bunch of other changes have gone into it and applying inline styles is now typically 30x+ faster. Many more changes in the changelog.

mudasobwa

mudasobwa

Creator of Cure

As a person who literally hates receiving emails in HTML, I am to suggest you to take a look at marcli which can be used to produce kinda formatted text messages out of a markdown template.

Actually, a support for markdown templates for HTML body might be a good addition to the library, too.

danschultzer

danschultzer OP

Pow Core Team

Yeah, HTML emails should always have plaintext alternatives. Premailex converts HTML to plaintext so you don’t have to maintain two versions yourself.

marcli looks cool, but not sure how markdown templates would fit in Premailex. Premailex is for processing already rendered HTML emails to ensure it can render correctly in email clients as well as producing plaintext alternative.

Over the years I have attempted different versions of mail templates in Phoenix and HTML is the simplest. Most recently it’s just been something like this:

defmodule MyAppWeb.Mail do
  import Swoosh.Email

  alias MyAppWeb.{Layouts, MailHTML}

  defp send_sample_email(user) do
    title = "Sample email"

    assigns =
      %{
        title: title,
        user: user
      }

    html = render_email(:sample, assigns)
    text = Premailex.to_text(html)

    new()
    |> from({"MyApp", "no-reply@notifications.example.com"})
    |> reply_to({"MyApp", "hello@example.com"})
    |> to(user.email)
    |> subject(title)
    |> html_body(html)
    |> text_body(text)
    |> deliver(:sample)
  end

  # ...

  defp render_email(type, assigns) do
    inner = Phoenix.Template.render(MailHTML, "#{type}", "html", assigns)
    root_assigns = assigns |> Map.put(:inner_content, inner) |> Map.delete(:layout)

    Layouts
    |> Phoenix.Template.render("mail", "html", root_assigns)
    |> Phoenix.HTML.Engine.encode_to_iodata!()
    |> IO.iodata_to_binary()
    |> Premailex.to_inline_css()
  end

  def deliver(mail, type) do
    mail
    |> put_provider_options()
    |> Mailer.deliver()
  end
end
defmodule MyAppWeb.MailHTML do
  @moduledoc false
  use MyAppWeb, :html

  embed_templates "mail_html/*"
end

With the mail.html.heex root template under components/layouts and everything else under mailers/. The root template has all styling in the style tag in <head>.

Markdown → HTML email would probably fit in this setup as well with Premailex used to ensure the rendered HTML has the applied inline styles.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
quatermain
Hello, I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself. I use Claude Code daily on a pr...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
LostKobrakai
I’ve recently created a small library phoenix_vite integrating the vite build tooling with phoenix. It provides an igniter.installer t...
New
danschultzer
I needed something like premailer for rails, and after searching, I decided to make an easy-to-use library to handle inline styles and co...
New
kraleppa
If you’ve ever had to debug a BEAM node you know it can be a painful process. Parsing raw data, or dealing with clutter often gets in the...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
CodeSync
Code BEAM Europe 2026 – Call for Talks is Open! We’re excited to announce that Code BEAM Europe 2026 is now open for talk submissions :t...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews