Phoenix.Swoosh (Phoenix.Swoosh v0.3.1) View Source
The main feature provided by this module is the ability to set the HTML and/or text body of an email by rendering templates.
It has been designed to integrate with Phoenix view, template and layout system.
Example
# web/templates/layout/email.html.eex
<html>
<head>
<title><%= @email.subject %></title>
</head>
<body>
<%= @inner_content %>
</body>
</html>
# web/templates/email/welcome.html.eex
<div>
<h1>Welcome to Sample, <%= @username %>!</h1>
</div>
# web/emails/user_email.ex
defmodule Sample.UserEmail do
use Phoenix.Swoosh, view: Sample.EmailView, layout: {Sample.LayoutView, :email}
def welcome(user) do
new()
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
end
Link to this section Summary
Functions
Retrieves the current layout of an email.
Stores the layout for rendering.
Stores the layout for rendering if one was not stored yet.
Stores the view for rendering if one was not stored yet.
Stores the view for rendering.
Renders the given template
and assigns
based on the email
.
Link to this section Functions
Retrieves the current layout of an email.
Stores the layout for rendering.
The layout must be a tuple, specifying the layout view and the layout
name, or false. In case a previous layout is set, put_layout
also
accepts the layout name to be given as a string or as an atom. If a
string, it must contain the format. Passing an atom means the layout
format will be found at rendering time, similar to the template in
render_body/3
. It can also be set to false
. In this case, no
layout would be used.
Examples
iex> layout(email)
false
iex> email = put_layout email, {LayoutView, "email.html"}
iex> layout(email)
{LayoutView, "email.html"}
iex> email = put_layout email, "email.html"
iex> layout(email)
{LayoutView, "email.html"}
iex> email = put_layout email, :email
iex> layout(email)
{AppView, :email}
Stores the layout for rendering if one was not stored yet.
Stores the view for rendering if one was not stored yet.
Stores the view for rendering.
Renders the given template
and assigns
based on the email
.
Once the template is rendered the resulting string is stored on the email fields html_body
and text_body
depending
on the format of the template.
Arguments
email
- theSwoosh.Email
structtemplate
- may be an atom or a string. If an atom, like:welcome
, it will render both the HTML and text template and stores them respectively on the email. If the template is a string it must contain the extension too, likewelcome.html
.assigns
- a dictionnary with the assigns to be used in the view. Those assigns are merged and have higher order precedence than the email assigns. (email.assigns
)
Example
defmodule Sample.UserEmail do
use Phoenix.Swoosh, view: Sample.EmailView
def welcome(user) do
%Email{}
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
end
The example above renders a template welcome.html
from Sample.EmailView
and
stores the resulting string onto the html_body field of the email.
(email.html_body
)
In many cases you may want to set both the html and text body of an email. To do so you can pass the template name as an atom (without the extension):
def welcome(user) do
%Email{}
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body(:welcome, %{username: user.email})
end
Layouts
Templates are often rendered inside layouts. If you wish to do so you will have
to specify which layout you want to use when using the Phoenix.Swoosh
module.
defmodule Sample.UserEmail do
use Phoenix.Swoosh, view: Sample.EmailView, layout: {Sample.LayoutView, :email}
def welcome(user) do
%Email{}
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
end
The example above will render the welcome.html
template inside an
email.html
template specified in Sample.LayoutView
. put_layout/2
can be
used to change the layout, similar to how put_view/2
can be used to change
the view.