ADR-028: Email templates: registered in code, overridden per workspace¶
Status: Accepted — see the Event v2 overview § SP4. Date: 2026-10-06
Builds on the mail outbox and ADR-027 (booking emails).
Problem¶
Transactional emails (booking confirmation, reminder, cancellation, website email verification) were English strings in Go. A workspace needs them in its visitors' languages and in its own words, without a deploy — and an admin-authored body must never become a way to break rendering, leak data or inject markup into mail clients.
Decision¶
- A module registers each email in code.
mail.RegisterTemplate(TemplateDef{Key, Label, Vars, Defaults})atinit(): a stable key (event.booking_confirmed), the variables the sender fills, and default texts per language (English required — the last fallback). The code owns which emails exist and what data they may show. - A workspace overrides (key, language). Table
mail_template(tenant_id,key,locale,subject,body_html; unique on the first three among live rows), off the generic CRUD surface:GET /api/v1/settings/mail_templates(catalog: definitions + this tenant's overrides) andPUT|DELETE /api/v1/settings/mail_templates/:key/:locale(DELETE = back to the default), permissionssettings:mail_templates:*derived from the route. - Substitution, not a template language.
{{var}}placeholders only, from the key's declared list; an unknown variable is refused on save (400). No conditionals or loops, so a saved template can't fail at send time, and an admin can't reach data the sender didn't hand over. Values are HTML-escaped in the HTML part and used as-is (one line) in the subject. - HTML is sanitized on save; text is derived. The body is edited in a WYSIWYG (
text/htmlfield widget, Tiptap) and reduced server-side to an allowlist (paragraphs, emphasis, lists, headings, links to http(s)/mailto/{{placeholder}}; scripts dropped with their content, every other attribute dropped). The plain-text part is generated from the HTML at send time, so the two can't drift. - Language = the recipient's.
ResolveLocale: the account'spreferred_locale, else the workspace default (i18n.default_locale), else English. Rendering walks locale → base language → English, and at each step a workspace override wins over the code default.
flowchart LR
S[Sender: booking, reminder sweep, signup] --> L[ResolveLocale]
L --> R[Render key + vars]
R --> O{override for fr?}
O -- yes --> F[fill + escape]
O -- no --> D{default for fr?}
D -- yes --> F
D -- no --> E[en: override, else default] --> F
F --> T[text = HTMLToText] --> Q[mail.Enqueue in the caller's tx] Consequences¶
- Adding an email = one
RegisterTemplatecall; it appears in Settings → Email templates with its variables as insertable chips. - Renaming or removing a variable in code silently empties it in existing overrides (they still render; the placeholder becomes ""). Treat a key's variables as a contract: add new ones, don't rename.
internal/mailcan't importinternal/settings(settings → auth → mail would cycle), so it reads the workspace language key as a literal. The default language is stored per company; an email isn't sent as a company, so the first company's setting stands for the workspace.- Dates are formatted by
mail.FormatDateTimewith built-in day/month names (en, fr): Go has no locale-aware formatting. Another language falls back to English names until added there.
Pitfalls¶
- Never build an email body by string concatenation any more: register a template so admins can reword and translate it.
- A link must be written as a placeholder (
href="{{cancel_url}}"): any other non-http(s) scheme is stripped on save.