Need help with phoenix_swoosh?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

swoosh
176 Stars 28 Forks MIT License 232 Commits 0 Opened issues

Description

Swoosh <3 Phoenix

Services available

!
?

Need anything else?

Contributors list

Phoenix.Swoosh

Elixir CI Module Version Hex Docs Total Download License Last Updated

Phoenix.View
+
Swoosh
.

This module provides the ability to set the HTML and/or text body of an email by rendering templates.

Installation

Add

:phoenix_swoosh
to your list of dependencies in
mix.exs
:
def deps do
  [
    {:phoenix_swoosh, "~> 1.0"},

# without phoenix_html, phoenix_swoosh only works with plain text templates
# if you want to use HTML templates
# {:phoenix_html, "~&gt; 3.0"},

] end

You probably also want to install

finch
,
hackney
or an HTTP client of your own choice, if you are using a provider that

Swoosh
talks to via their HTTP API.

Or

:gen_smtp
if you are working with a provider that only works through SMTP.

See

Swoosh
for more details.

Usage

1. Classic setup

Setting up the templates:

# path_to/templates/user_notifier/welcome.html.eex

Welcome to Sample, !

# path_to/views/user_notifier_view.ex
defmodule Sample.UserNotifierView do
  use Phoenix.View, root: "path_to/templates"
end

Passing values to templates:

# path_to/notifiers/user_notifier.ex
defmodule Sample.UserNotifier do
  use Phoenix.Swoosh, view: Sample.UserNotifierView

def welcome(user) do new() |> from("[email protected]") |> to(user.email) |> subject("Hello, Avengers!") |> render_body("welcome.html", %{name: name}) end end

Maybe with a layout:

# path_to/templates/layout/email.html.eex


<title></title>

defmodule Sample.LayoutView do
  use Phoenix.View, root: "path_to/templates"
end
# path_to/notifiers/user_notifier.ex
defmodule Sample.UserNotifier do
  use Phoenix.Swoosh,
    view: Sample.NotifierView,
    layout: {Sample.LayoutView, :email}

... same welcome ...

end

Layout can also be added/changed dynamically with

put_new_layout/2
and
put_layout/2

2. Standalone setup

# path_to/templates/user_notifier/welcome.html.eex

Welcome to Sample, !

# path_to/notifiers/user_notifier.ex
defmodule Sample.UserNotifier do
  use Phoenix.Swoosh,
    template_root: "path_to/templates",
    template_path: "user_notifier"

... same welcome ...

end

In this setup, the notifier module itself serves as the view module

template_root
,
template_path
and
template_namespace
will be passed to
Phoenix.View
as
root
,
path
and
namespace
.

Layout can be setup the same way as classic setup.

Copyright and License

Copyright (c) 2021 Swoosh contributors

Released under the MIT License, which can be found in LICENSE.md.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.