Bamboo.Phoenix (bamboo_phoenix v1.0.0) View Source

Render emails with Phoenix templates and layouts.

This module allows rendering emails with Phoenix layouts and views. Pass an atom (e.g. :welcome_email) as the template name to render both HTML and plain text emails. Use a string if you only want to render one type, e.g. "welcome_email.text" or "welcome_email.html".

Examples

Set the text and HTML layout for an email.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def welcome_email do
    new_email()
    |> put_text_layout({MyAppWeb.LayoutView, "email.text"})
    |> put_html_layout({MyAppWeb.LayoutView, "email.html"})
    |> render(:welcome) # Pass atom to render html AND plain text templates
  end
end

Set both the text and HTML layout at the same time for an email.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def welcome_email do
    new_email()
    |> put_layout({MyAppWeb.LayoutView, :email})
    |> render(:welcome)
  end
end

Render both text and html emails without layouts.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def welcome_email do
    new_email()
    |> render(:welcome)
  end
end

Make assigns available to a template.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def welcome_email(user) do
    new_email()
    |> assign(:user, user)
    |> render(:welcome)
  end
end

Make assigns available to a template during render call.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def welcome_email(user) do
    new_email()
    |> put_html_layout({MyAppWeb.LayoutView, "email.html"})
    |> render(:welcome, user: user)
  end
end

Render an email by passing the template string to render.

defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def html_email do
    new_email
    |> render("html_email.html")
  end

  def text_email do
    new_email
    |> render("text_email.text")
  end
end

HTML Layout Example

# my_app_web/email.ex
defmodule MyApp.Email do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def sign_in_email(person) do
    base_email()
    |> to(person)
    |> subject("Your Sign In Link")
    |> assign(:person, person)
    |> render(:sign_in)
  end

  defp base_email do
    new_email
    |> from("Rob Ot<robot@changelog.com>")
    |> put_header("Reply-To", "editors@changelog.com")
    # This will use the "email.html.eex" file as a layout when rendering html emails.
    # Plain text emails will not use a layout unless you use `put_text_layout`
    |> put_html_layout({MyAppWeb.LayoutView, "email.html"})
  end
end

# my_app_web/views/email_view.ex
defmodule MyAppWeb.EmailView do
  use MyAppWeb, :view
end

# my_app_web/templates/layout/email.html.eex
<html>
  <head>
    <link rel="stylesheet" href="<%= static_url(MyApp.Endpoint, "/css/email.css") %>">
  </head>
  <body>
    <%= render @view_module, @view_template, assigns %>
  </body>
</html>

# my_app_web/templates/email/sign_in.html.eex
<p><%= link "Sign In", to: sign_in_url(MyApp.Endpoint, :create, @person) %></p>

# my_app_web/templates/email/sign_in.text.eex
# This will not be rendered within a layout because `put_text_layout` was not used.
Sign In: <%= sign_in_url(MyApp.Endpoint, :create, @person) %>

Link to this section Summary

Functions

Sets an assign for the email. These will be available when rendering the email

Sets the layout when rendering HTML templates.

Sets the layout for rendering plain text and HTML templates.

Sets the layout when rendering plain text templates.

Render a Phoenix template and set the body on the email.

Link to this section Functions

Link to this function

assign(email, key, value)

View Source

Sets an assign for the email. These will be available when rendering the email

Link to this function

put_html_layout(email, layout)

View Source

Sets the layout when rendering HTML templates.

Example

def html_email_layout do
  new_email
  # Will use MyAppWeb.LayoutView with email.html template when rendering html emails
  |> put_html_layout({MyAppWeb.LayoutView, "email.html"})
end

Sets the layout for rendering plain text and HTML templates.

Example

def text_and_html_email_layout do
  new_email
  # Will use MyAppWeb.LayoutView with the email.html template for html emails
  # and MyAppWeb.LayoutView with the email.text template for text emails
  |> put_layout({MyAppWeb.LayoutView, :email})
end
Link to this function

put_text_layout(email, layout)

View Source

Sets the layout when rendering plain text templates.

Example

def text_email_layout do
  new_email
  # Will use MyAppWeb.LayoutView with email.text template when rendering text emails
  |> put_text_layout({MyAppWeb.LayoutView, "email.text"})
end
Link to this function

render(email, template_name, assigns)

View Source

Render a Phoenix template and set the body on the email.

Pass an atom as the template name to render HTML and plain text emails, e.g. :welcome_email. Use a string if you only want to render one type, e.g. "welcome_email.text" or "welcome_email.html". Scroll to the top for more examples.