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
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
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
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

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
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews