Phoenix.Swoosh (Phoenix.Swoosh v1.2.1)
View SourceThe main feature provided by this module is the ability to set the HTML and/or text body of an email by rendering templates.
It utilizes Phoenix.View and can work very well both standalone
and in apps using Phoenix framework.
use Phoenix.Swoosh also accepts a list of options which configures where
templates will be looked up:
:view- for "classic" setup, the view module to use for rendering:template_path- for "standalone" setup, given toPhoenix.Viewaspath:template_root- for "standalone" setup, given toPhoenix.Viewasroot:template_namespace- for "standalone" setup, given toPhoenix.Viewasnamespace:layout- the layout to render the templates in. Must be a tuple, specifying the layout view and the layout name, orfalse:formats- to customize the extensions of the templates. Must be a map, with key being the extension and the value being the body field to set
Summary
Functions
Retrieves the current layout of an email.
Stores the layout for rendering.
Stores the formats for rendering if none was stored yet.
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.
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 formats for rendering if none was stored yet.
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.
.html, .htm, and .xml are stored in html_body; all other extensions,
(e.g. .txt and .text), in text_body.
Arguments
email- theSwoosh.Emailstruct.template- 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 dictionary with the assigns to be used in the view. Those assigns are merged and have higher order precedence than the email assigns. (email.assigns)
Examples
defmodule Sample.UserEmail do
use Phoenix.Swoosh, view: Sample.EmailView
def welcome(user) do
new()
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
endThe 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
new()
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body(:welcome, %{username: user.email})
endLayouts
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
new()
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
endThe 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.