fix emails

This commit is contained in:
2024-07-28 11:35:45 -04:00
parent 179d67a896
commit 33e4d26a3d
6 changed files with 50 additions and 5 deletions

View File

@ -1,61 +0,0 @@
defmodule Cannery.Email do
@moduledoc """
Emails that can be sent using Swoosh.
You can find the base email templates at
`lib/cannery_web/components/layouts/email_html.html.heex` for html emails and
`lib/cannery_web/components/layouts/email_text.txt.eex` for text emails.
"""
import Swoosh.Email
import CanneryWeb.Gettext
import Phoenix.Template
alias Cannery.Accounts.User
alias CanneryWeb.{EmailHTML, Layouts}
@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
from = Application.get_env(:cannery, Cannery.Mailer)[:email_from] || "noreply@localhost"
name = Application.get_env(:cannery, Cannery.Mailer)[:email_name]
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
|> base_email(dgettext("emails", "Confirm your Cannery account"))
|> html_email(:confirm_email_html, %{user: user, url: url})
|> text_email(:confirm_email_text, %{user: user, url: url})
end
def generate_email("reset_password", user, %{url: url}) do
user
|> base_email(dgettext("emails", "Reset your Cannery password"))
|> html_email(:reset_password_html, %{user: user, url: url})
|> text_email(:reset_password_text, %{user: user, url: url})
end
def generate_email("update_email", user, %{url: url}) do
user
|> base_email(dgettext("emails", "Update your Cannery email"))
|> html_email(:update_email_html, %{user: user, url: url})
|> text_email(:update_email_text, %{user: user, url: url})
end
defp html_email(email, atom, assigns) do
heex = apply(EmailHTML, atom, [assigns])
html = render_to_string(Layouts, "email_html", "html", email: email, inner_content: heex)
email |> html_body(html)
end
defp text_email(email, atom, assigns) do
heex = apply(EmailHTML, atom, [assigns])
text = render_to_string(Layouts, "email_text", "text", email: email, inner_content: heex)
email |> text_body(text)
end
end

View File

@ -1,13 +0,0 @@
defmodule Cannery.EmailWorker do
@moduledoc """
Oban worker that dispatches emails
"""
use Oban.Worker, queue: :mailers, tags: ["email"]
alias Cannery.{Accounts, Email, Mailer}
@impl Oban.Worker
def perform(%Oban.Job{args: %{email: email, user_id: user_id, attrs: attrs}}) do
Email.generate_email(email, user_id |> Accounts.get_user!(), attrs) |> Mailer.deliver()
end
end