forked from shibao/cannery
add swoosh and oban
This commit is contained in:
54
lib/cannery/accounts/email.ex
Normal file
54
lib/cannery/accounts/email.ex
Normal file
@ -0,0 +1,54 @@
|
||||
defmodule Cannery.Email do
|
||||
@moduledoc """
|
||||
Emails that can be sent using Swoosh.
|
||||
|
||||
You can find the base email templates at
|
||||
`lib/cannery_web/templates/layout/email.html.heex` for html emails and
|
||||
`lib/cannery_web/templates/layout/email.txt.heex` for text emails.
|
||||
"""
|
||||
|
||||
use Phoenix.Swoosh, view: Cannery.EmailView, layout: {Cannery.LayoutView, :email}
|
||||
import CanneryWeb.Gettext
|
||||
alias Cannery.Accounts.User
|
||||
alias CanneryWeb.EmailView
|
||||
|
||||
@typedoc """
|
||||
Represents an HTML and text body email that can be sent
|
||||
"""
|
||||
@type t() :: Swoosh.Email.t()
|
||||
|
||||
@spec base_email(User.t(), String.t()) :: t()
|
||||
defp base_email(%User{email: email}, subject) do
|
||||
new()
|
||||
|> to(email)
|
||||
|> from({
|
||||
Application.get_env(:cannery, Cannery.Mailer)[:email_name],
|
||||
Application.get_env(:cannery, Cannery.Mailer)[:email_from]
|
||||
})
|
||||
|> subject(subject)
|
||||
end
|
||||
|
||||
@spec welcome_email(User.t(), String.t()) :: t()
|
||||
def welcome_email(user, url) do
|
||||
user
|
||||
|> base_email(dgettext("emails", "Confirm your %{name} account", name: "Cannery"))
|
||||
|> render_body("confirm_email.html", %{user: user, url: url})
|
||||
|> text_body(EmailView.render("confirm_email.txt", %{user: user, url: url}))
|
||||
end
|
||||
|
||||
@spec reset_password_email(User.t(), String.t()) :: t()
|
||||
def reset_password_email(user, url) do
|
||||
user
|
||||
|> base_email(dgettext("emails", "Reset your %{name} password", name: "Cannery"))
|
||||
|> render_body("reset_password.html", %{user: user, url: url})
|
||||
|> text_body(EmailView.render("reset_password.txt", %{user: user, url: url}))
|
||||
end
|
||||
|
||||
@spec update_email(User.t(), String.t()) :: t()
|
||||
def update_email(user, url) do
|
||||
user
|
||||
|> base_email(dgettext("emails", "Update your %{name} email", name: "Cannery"))
|
||||
|> render_body("update_email.html", %{user: user, url: url})
|
||||
|> text_body(EmailView.render("update_email.txt", %{user: user, url: url}))
|
||||
end
|
||||
end
|
9
lib/cannery/accounts/email_worker.ex
Normal file
9
lib/cannery/accounts/email_worker.ex
Normal file
@ -0,0 +1,9 @@
|
||||
defmodule Cannery.EmailWorker do
|
||||
use Oban.Worker, queue: :mailers
|
||||
alias Cannery.Mailer
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Oban.Job{args: email}) do
|
||||
email |> Mailer.deliver()
|
||||
end
|
||||
end
|
@ -1,77 +0,0 @@
|
||||
defmodule Cannery.Accounts.UserNotifier do
|
||||
@moduledoc """
|
||||
Contains all user emails and notifications
|
||||
"""
|
||||
|
||||
# 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
|
||||
|
||||
@doc """
|
||||
Deliver instructions to confirm account.
|
||||
"""
|
||||
def deliver_confirmation_instructions(user, url) do
|
||||
deliver(user.email, """
|
||||
|
||||
==============================
|
||||
|
||||
Hi #{user.email},
|
||||
|
||||
You can confirm your account by visiting the URL below:
|
||||
|
||||
#{url}
|
||||
|
||||
If you didn't create an account with us, please ignore this.
|
||||
|
||||
==============================
|
||||
""")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deliver instructions to reset a user password.
|
||||
"""
|
||||
def deliver_reset_password_instructions(user, url) do
|
||||
deliver(user.email, """
|
||||
|
||||
==============================
|
||||
|
||||
Hi #{user.email},
|
||||
|
||||
You can reset your password by visiting the URL below:
|
||||
|
||||
#{url}
|
||||
|
||||
If you didn't request this change, please ignore this.
|
||||
|
||||
==============================
|
||||
""")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deliver instructions to update a user email.
|
||||
"""
|
||||
def deliver_update_email_instructions(user, url) do
|
||||
deliver(user.email, """
|
||||
|
||||
==============================
|
||||
|
||||
Hi #{user.email},
|
||||
|
||||
You can change your email by visiting the URL below:
|
||||
|
||||
#{url}
|
||||
|
||||
If you didn't request this change, please ignore this.
|
||||
|
||||
==============================
|
||||
""")
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user