implemented bamboo mailer, satisfying tests
This commit is contained in:
parent
613ab2c80a
commit
370ac77cfd
9 changed files with 57 additions and 19 deletions
lib
bones73k
bones73k_web/controllers
|
@ -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
3
lib/bones73k/mailer.ex
Normal file
|
@ -0,0 +1,3 @@
|
|||
defmodule Bones73k.Mailer do
|
||||
use Bamboo.Mailer, otp_app: :bones73k
|
||||
end
|
17
lib/bones73k/mailer/user_email.ex
Normal file
17
lib/bones73k/mailer/user_email.ex
Normal 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
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue