Files
chess-endgame-trainer/lib/chesstrainer/mail.ex
T
owenrees 37e0ac8e56 Transactional mail, crawler metadata, and asset measurement (#179)
* Send transactional email from no-reply@mail.innersquare.app

Centralise the sender identity, reply-to address and SES configuration set in Chesstrainer.Mail so no notifier can drift. Add MAIL_REDIRECT_TO for smoke tests and a mix mail.check task that exercises the SES mailbox simulators, and document the setup in the deploy guide and .env.example.

* Tighten crawler metadata and keep play pages out of search

Canonical URLs no longer carry query strings, public pages get an OG image, WebSite JSON-LD and a per-page language, and the sitemap dates the landing page only. Play and guest-training routes are marked noindex so tens of thousands of near-identical pages stay out of the index.

* Measure built asset sizes with mix assets.sizes

Report raw, gzip and brotli sizes for built JS and CSS, and record the current numbers plus the board-code split recommendation in docs/assets.md.
2026-11-17 10:29:45 +01:00

83 lines
2.6 KiB
Elixir

defmodule Chesstrainer.Mail do
@moduledoc """
House rules for every outgoing email.
Notifiers build the message body and hand it here, so the sender identity,
reply-to address, SES configuration set and the test redirect are applied in
exactly one place and no sender can forget them.
The From address lives on the `mail.` subdomain so transactional mail is
distinct from the addresses that receive mail on the apex domain. SES
verification of the parent domain covers the subdomain, so no separate
identity is required for the From header to be accepted.
"""
alias Chesstrainer.Branding
alias Chesstrainer.Mailer
@default_configuration_set "my-first-configuration-set"
@doc """
Applies the house rules and delivers the email.
Returns `{:ok, email}` so notifiers can report the message they sent.
"""
def deliver(%Swoosh.Email{} = email) do
email = prepare(email)
with {:ok, _metadata} <- Mailer.deliver(email) do
{:ok, email}
end
end
@doc """
Applies the house rules without delivering.
Exposed so tooling (such as `mix mail.check`) can inspect or send the exact
message the application would send.
"""
def prepare(%Swoosh.Email{} = email) do
email
|> Swoosh.Email.from({Branding.name(), Branding.mail_from()})
|> Swoosh.Email.reply_to(Branding.support_email())
|> Swoosh.Email.put_provider_option(:configuration_set_name, configuration_set())
|> redirect()
end
defp configuration_set do
Application.get_env(:chesstrainer, :ses_configuration_set, @default_configuration_set)
end
# Rewrites every recipient to one address so a staging or production smoke
# test can send through real SES without reaching users. The original
# recipients stay in the subject and in an `X-Original-To` header so the
# redirected message explains itself.
defp redirect(email) do
case Application.get_env(:chesstrainer, :mail_redirect_to) do
address when is_binary(address) and address != "" ->
originals = recipients(email)
require Logger
Logger.warning("mail redirect: #{Enum.join(originals, ", ")} -> #{address}")
%{email | to: [{"", address}], cc: [], bcc: []}
|> Swoosh.Email.header("X-Original-To", Enum.join(originals, ", "))
|> Swoosh.Email.subject("[→ #{Enum.join(originals, ", ")}] #{email.subject}")
_unset ->
email
end
end
defp recipients(email) do
[email.to, email.cc, email.bcc]
|> List.flatten()
|> Enum.reject(&is_nil/1)
|> Enum.map(fn
{_name, address} -> address
address when is_binary(address) -> address
end)
end
end