PhoenixEmail (PhoenixEmail v0.1.1)

Copy Markdown View Source

Build emails with HEEx components. A port of react-email for Phoenix.

Define emails as regular function components and render them to an HTML string (or a plain text version for multipart emails):

defmodule MyApp.Emails do
  use PhoenixEmail

  def welcome(assigns) do
    ~H"""
    <.email>
      <.head />
      <.preview>You're in — let's get you set up</.preview>
      <.body style="background-color:#ffffff;font-family:sans-serif">
        <.container>
          <.heading as="h1">Hello {@name}</.heading>
          <.text>Thanks for signing up.</.text>
          <.button href={@url} style="background-color:#5e6ad2;color:#fff;padding:12px 20px;border-radius:8px">
            Get started
          </.button>
          <.hr />
          <.link href="https://example.com">example.com</.link>
        </.container>
      </.body>
    </.email>
    """
  end
end

html = PhoenixEmail.render(&MyApp.Emails.welcome/1, %{name: "Ada", url: "https://example.com/start"})
text = PhoenixEmail.render(&MyApp.Emails.welcome/1, %{name: "Ada", url: "https://example.com/start"}, plain_text: true)

use PhoenixEmail pulls in Phoenix.Component and imports all the components from PhoenixEmail.Components (Phoenix's link/1 is excluded in favor of the email one).

Summary

Functions

Renders an email to a string.

Renders a function component to a string with the given assigns.

Functions

render(rendered_or_fun, assigns_or_opts \\ [])

Renders an email to a string.

Accepts either the result of a ~H template or a function component plus its assigns. Returns the HTML prefixed with the XHTML 1.0 Transitional doctype (the same one react-email emits).

Options

  • :plain_text - when true, returns a plain text version of the email instead of HTML, for the text/plain part of multipart messages.

render(fun, assigns, opts)

Renders a function component to a string with the given assigns.

See render/2 for the options.