Preflight for your HTML emails. Inlines CSS styles and converts HTML to plain text.
Features
- Inline CSS from
<style>tags - Inline CSS from external
<link>stylesheets - Convert HTML to plain text
Usage
Convert an HTML string to plain text:
Premailex.to_text(html)Inline an HTML string with CSS styles defined in <head>:
Premailex.to_inline_css(html)Example with Swoosh
def welcome(user) do
new()
|> to({user.name, user.email})
|> from({"Dr B Banner", "hulk.smash@example.com"})
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.username})
|> 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)
endExample with Bamboo
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)
endHTML parser
Premailex supports LazyHTML, Floki, Meeseeks, and :xmerl.
It automatically selects the first available parser based on your mix.exs dependencies:
defp deps do
[
{:premailex, "~> 1.0"},
# {:lazy_html, "~> 0.1.11"},
# {:floki, "~> 0.24"},
# {:meeseeks, "~> 0.11"}
]
endTo explicitly configure which parser to use, add to your config.exs:
config :premailex, html_parser: Premailex.HTMLParser.Meeseeks
# or
config :premailex, html_parser: Premailex.HTMLParser.LazyHTML
Summary
Functions
Parses an HTML string into a html_tree/0.
Converts a html_tree/0 into an HTML string.
Adds inline styles to an HTML string or html_tree/0.
Converts an HTML string or html_tree/0 to plain text.
Types
Functions
Parses an HTML string into a html_tree/0.
Options
:html_parser- HTML parser to use (seePremailex.HTMLParser);
Examples
iex> Premailex.parse(~s(<p class="lead">Hello</p>))
[{"p", [{"class", "lead"}], ["Hello"]}]
Converts a html_tree/0 into an HTML string.
Options
:html_parser- HTML parser to use (seePremailex.HTMLParser);
Examples
iex> Premailex.to_html([{"p", [{"class", "lead"}], ["Hello"]}])
~s(<p class="lead">Hello</p>)
Adds inline styles to an HTML string or html_tree/0.
Options
:html_parser- HTML parser to use (seePremailex.HTMLParser);:http_adapter- HTTP adapter to use for fetching external stylesheets (seePremailex.HTTPAdapter);:remove_style_tags- whether to remove the<style>and<link>tags after inlining. Defaults to false;
Examples
iex> Premailex.to_inline_css(
...> ~s(<html><head>
...> <style>p{background-color: #fff;}</style>
...> </head><body>
...> <p style="color: #000;">Text</p>
...> </body></html>))
~s(<html><head>
<style>p{background-color: #fff;}</style>
</head><body>
<p style="background-color: #fff; color: #000;">Text</p>
</body></html>)
Converts an HTML string or html_tree/0 to plain text.
Options
:html_parser- HTML parser to use (seePremailex.HTMLParser);
Examples
iex> Premailex.to_text(
...> ~s(<html><head>
...> <style>p{background-color:#fff;}</style>
...> </head><body>
...> <p style="color:#000;">Text</p>
...> </body></html>))
"Text"