memEx/lib/memex/accounts/email.ex

49 lines
1.8 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule Memex.Email do
2022-02-25 21:35:12 -05:00
@moduledoc """
Emails that can be sent using Swoosh.
You can find the base email templates at
2022-07-25 19:31:54 -04:00
`lib/memex_web/templates/layout/email.html.heex` for html emails and
`lib/memex_web/templates/layout/email.txt.heex` for text emails.
2022-02-25 21:35:12 -05:00
"""
2022-07-25 19:31:54 -04:00
use Phoenix.Swoosh, view: MemexWeb.EmailView, layout: {MemexWeb.LayoutView, :email}
import MemexWeb.Gettext
alias Memex.Accounts.User
alias MemexWeb.EmailView
2022-02-25 21:35:12 -05:00
@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
2022-07-25 19:31:54 -04:00
from = Application.get_env(:Memex, Memex.Mailer)[:email_from] || "noreply@localhost"
name = Application.get_env(:Memex, Memex.Mailer)[:email_name]
2022-02-25 21:35:12 -05:00
new() |> to(email) |> from({name, from}) |> subject(subject)
end
@spec generate_email(key :: String.t(), User.t(), attrs :: map()) :: t()
def generate_email("welcome", user, %{"url" => url}) do
user
2022-07-25 19:31:54 -04:00
|> base_email(dgettext("emails", "Confirm your Memex account"))
2022-02-25 21:35:12 -05:00
|> render_body("confirm_email.html", %{user: user, url: url})
|> text_body(EmailView.render("confirm_email.txt", %{user: user, url: url}))
end
def generate_email("reset_password", user, %{"url" => url}) do
user
2022-07-25 19:31:54 -04:00
|> base_email(dgettext("emails", "Reset your Memex password"))
2022-02-25 21:35:12 -05:00
|> render_body("reset_password.html", %{user: user, url: url})
|> text_body(EmailView.render("reset_password.txt", %{user: user, url: url}))
end
def generate_email("update_email", user, %{"url" => url}) do
user
2022-07-25 19:31:54 -04:00
|> base_email(dgettext("emails", "Update your Memex email"))
2022-02-25 21:35:12 -05:00
|> render_body("update_email.html", %{user: user, url: url})
|> text_body(EmailView.render("update_email.txt", %{user: user, url: url}))
end
end