implemented bamboo mailer, satisfying tests

This commit is contained in:
Adam Piontek 2021-02-24 15:52:42 -05:00
parent 613ab2c80a
commit 370ac77cfd
9 changed files with 57 additions and 19 deletions
lib
bones73k
bones73k_web/controllers

View file

@ -1,21 +1,13 @@
defmodule Bones73k.Accounts.UserNotifier do
# For simplicity, this module simply logs messages to the terminal.
# You should replace it by a proper email or notification tool, such as:
#
# * Swoosh - https://hexdocs.pm/swoosh
# * Bamboo - https://hexdocs.pm/bamboo
#
defp deliver(to, body) do
require Logger
Logger.debug(body)
{:ok, %{to: to, body: body}}
end
alias Bones73k.Mailer
alias Bones73k.Mailer.UserEmail
@doc """
Deliver instructions to confirm account.
"""
def deliver_confirmation_instructions(user, url) do
deliver(user.email, """
user
|> UserEmail.compose("Confirm Your Account", """
==============================
@ -29,13 +21,15 @@ defmodule Bones73k.Accounts.UserNotifier do
==============================
""")
|> Mailer.deliver_later()
end
@doc """
Deliver instructions to reset a user password.
"""
def deliver_reset_password_instructions(user, url) do
deliver(user.email, """
user
|> UserEmail.compose("Reset Your Password", """
==============================
@ -49,13 +43,15 @@ defmodule Bones73k.Accounts.UserNotifier do
==============================
""")
|> Mailer.deliver_later()
end
@doc """
Deliver instructions to update a user email.
"""
def deliver_update_email_instructions(user, url) do
deliver(user.email, """
user
|> UserEmail.compose("Change Your E-mail", """
==============================
@ -69,5 +65,6 @@ defmodule Bones73k.Accounts.UserNotifier do
==============================
""")
|> Mailer.deliver_later()
end
end

3
lib/bones73k/mailer.ex Normal file
View file

@ -0,0 +1,3 @@
defmodule Bones73k.Mailer do
use Bamboo.Mailer, otp_app: :bones73k
end

View file

@ -0,0 +1,17 @@
defmodule Bones73k.Mailer.UserEmail do
import Bamboo.Email
@mailer_vars Application.get_env(:bones73k, :app_global_vars,
mailer_reply_to: "admin@example.com",
mailer_from: {"Bones73k", "bones73k@example.com"}
)
def compose(user, subject, body_text) do
new_email()
|> from(@mailer_vars[:mailer_from])
|> to(user.email)
|> put_header("Reply-To", @mailer_vars[:mailer_reply_to])
|> subject(subject)
|> text_body(body_text)
end
end

View file

@ -13,7 +13,7 @@ defmodule Bones73kWeb.UserRegistrationController do
def create(conn, %{"user" => user_params}) do
case Accounts.register_user(user_params) do
{:ok, user} ->
{:ok, _} =
%Bamboo.Email{} =
Accounts.deliver_user_confirmation_instructions(
user,
&Routes.user_confirmation_url(conn, :confirm, &1)