refactor(email): share build/deliver skeleton across join emails

This commit is contained in:
Moritz 2026-06-16 15:10:03 +02:00 committed by moritz
parent 1adf6aa664
commit e66fb5d3d9
4 changed files with 61 additions and 65 deletions

View file

@ -0,0 +1,54 @@
defmodule MvWeb.Emails.JoinEmail do
@moduledoc """
Shared build/deliver skeleton for the join-flow emails (confirmation,
already-a-member, already-pending).
Each concrete join email supplies only its subject, template, and any
email-specific assigns; this module builds the Swoosh email with the unified
layout and delivers it via `Mailer.deliver/2` with `Mailer.smtp_config/0`,
preserving the `{:ok, email}` / `{:error, reason}` contract.
"""
use Phoenix.Swoosh,
view: MvWeb.EmailsView,
layout: {MvWeb.EmailLayoutView, "layout.html"}
import Swoosh.Email
alias Mv.Mailer
@doc """
Builds and delivers a join-flow email.
- `email_address` - recipient address
- `template` - the EmailsView template to render (e.g. `"join_confirmation.html"`)
- `subject` - already-translated subject line
- `extra_assigns` - email-specific assigns merged on top of the common ones
(`subject`, `app_name`, `locale`)
Returns `{:ok, email}` on success, `{:error, reason}` on delivery failure.
"""
@spec deliver(String.t(), String.t(), String.t(), map()) ::
{:ok, Swoosh.Email.t()} | {:error, term()}
def deliver(email_address, template, subject, extra_assigns \\ %{})
when is_binary(email_address) and is_binary(template) and is_binary(subject) do
assigns =
Map.merge(
%{
subject: subject,
app_name: Mailer.mail_from() |> elem(0),
locale: Gettext.get_locale(MvWeb.Gettext)
},
extra_assigns
)
email =
new()
|> from(Mailer.mail_from())
|> to(email_address)
|> subject(subject)
|> put_view(MvWeb.EmailsView)
|> render_body(template, assigns)
Mailer.deliver(email, Mailer.smtp_config())
end
end