Phoenix.Swoosh

Elixir CIModule VersionHex DocsTotal DownloadLicenseLast Updated

Use Swoosh to easily send emails in your Phoenix project.

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, "~> 0.3"}
  ]
end

Usage

Setting up the templates:

# 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>

Passing values to templates:

# 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

Copyright and License

Copyright (c) 2016 Swoosh contributors

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