feat(email): every email Minstrel sends uses one layout (#8150)

The password reset, the new-music and activity summaries and the SMTP
test message each drew their own page; the test message had no layout
at all. They now share internal/mailer's layout: the Minstrel name at
the top, the email's heading and message on a card in the design
system's dark theme, and a footer saying which server sent it and why,
signed "— Minstrel", with the notification-settings link on the
summaries. A plain-text body in the same frame goes alongside.

An email supplies only its content templates and a Frame (heading,
server, reason). Links, buttons and quiet text take named styles from
the layout ({{style "link"}}), so colours change in one place; an
unknown style name fails the render. The SMTP test message is a
template now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 10:27:16 -05:00
co-authored by Claude Opus 5.5
parent a18d5aaa11
commit bae3421057
14 changed files with 331 additions and 113 deletions
+147
View File
@@ -0,0 +1,147 @@
package mailer
import (
"bytes"
"embed"
"fmt"
htmltemplate "html/template"
"io/fs"
"strings"
texttemplate "text/template"
)
// Every email Minstrel sends is drawn by one layout: the Minstrel name at the
// top, the email's heading and message on a card, and a footer saying which
// server sent it and why. An email supplies only its content, as a text
// template and an HTML template, plus a Frame; Render puts the content in the
// frame. A plain-text body goes out beside the HTML for clients that won't
// show HTML. Colours and type come from the Minstrel
// design system's dark theme, the app's own look, so an email reads as part
// of the same product on every client.
//go:embed templates/layout.html templates/layout.txt
var layoutFS embed.FS
var (
layoutText = texttemplate.Must(texttemplate.ParseFS(layoutFS, "templates/layout.txt"))
layoutHTML = htmltemplate.Must(htmltemplate.New("layout.html").Funcs(htmltemplate.FuncMap{"style": style}).
ParseFS(layoutFS, "templates/layout.html"))
)
// The Minstrel design system's values, as an email client needs them: inline,
// and with web fonts named first but never required.
const (
colourPage = "#14171A" // --fs-surface-page
colourCard = "#1E2228" // --fs-surface-raised
colourBorder = "#3F4651" // --fs-border-color
colourText = "#E8E4D8" // --fs-text-primary
colourSecondary = "#C2BFB4" // --fs-text-secondary
colourTertiary = "#9C9A92" // --fs-text-tertiary
colourLink = "#A1AEA0" // --fs-accent-fg: the accent, readable as text on a dark card
colourAction = "#4A5D3F" // --fs-action-primary
colourOnAction = "#E8E4D8" // --fs-text-on-action
fontBody = "'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif"
fontDisplay = "'Fraunces', Georgia, 'Times New Roman', serif"
fontMono = "'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace"
)
// styles are the named inline styles an email's HTML content uses, written
// `style="{{style "link"}}"`, so every email's links, buttons and quiet text
// look alike and change in one place.
var styles = map[string]string{
"page": "margin:0; padding:0; background:" + colourPage + ";",
"wrap": "background:" + colourPage + ";",
"brand": "padding:0 4px 16px; font-family:" + fontDisplay + "; font-size:22px; font-weight:500; color:" + colourText + ";",
"heading": "margin:0 0 20px; font-family:" + fontDisplay + "; font-size:20px; font-weight:500; line-height:1.3; color:" + colourText + ";",
"card": "background:" + colourCard + "; border:1px solid " + colourBorder + "; border-radius:12px; padding:28px; font-family:" + fontBody + "; font-size:15px; line-height:1.55; color:" + colourText + ";",
"footer": "padding:16px 4px 0; font-family:" + fontBody + "; font-size:12px; line-height:1.5; color:" + colourTertiary + ";",
"p": "margin:0 0 16px;",
"link": "color:" + colourLink + "; text-decoration:underline;",
"quiet": "color:" + colourSecondary + "; font-size:14px;",
"faint": "color:" + colourTertiary + ";",
"group": "margin:20px 0 4px; font-weight:600; color:" + colourText + ";",
"list": "margin:0 0 8px; padding-left:20px;",
"item": "margin:2px 0;",
"button": "display:inline-block; padding:12px 24px; background:" + colourAction + "; color:" + colourOnAction + "; text-decoration:none; border-radius:8px; font-weight:500;",
"code": "font-family:" + fontMono + "; font-size:13px; word-break:break-all; color:" + colourSecondary + ";",
}
// style returns a named style for an inline style attribute. An unknown name
// fails the render, so a typo shows in the email's test, not in an inbox.
func style(name string) (htmltemplate.CSS, error) {
s, ok := styles[name]
if !ok {
return "", fmt.Errorf("mailer: no email style %q", name)
}
return htmltemplate.CSS(s), nil //nolint:gosec // fixed strings from the table above, never user input
}
// Frame is what the layout says around an email's content.
type Frame struct {
// Heading is the line at the top of the card, saying what the email is.
Heading string
// Server is the server's public address, named in the footer as the
// sender; empty when none is set, and the footer says "your Minstrel
// server" instead.
Server string
// Reason finishes the footer's "…because": why this person got this
// email, in a clause ("you asked to reset your password").
Reason string
// NotificationSettings adds where to choose which notifications are
// emailed: a link under Server when it is set, else where to find it in
// the app.
NotificationSettings bool
}
// SettingsURL is the notification settings page under Server, or "".
func (f Frame) SettingsURL() string {
if f.Server == "" {
return ""
}
return strings.TrimRight(f.Server, "/") + "/settings/notifications"
}
// ServerName is how the footer names the sender.
func (f Frame) ServerName() string {
if f.Server == "" {
return "your Minstrel server"
}
return "the Minstrel server at " + strings.TrimRight(f.Server, "/")
}
type layoutVars struct {
Content any
Frame Frame
}
// Render returns an email's text and HTML bodies: the content templates
// textName and htmlName from content, executed with vars, inside the shared
// layout, framed by frame. HTML content styles its elements with
// {{style "name"}}.
func Render(content fs.FS, textName, htmlName string, vars any, frame Frame) (string, string, error) {
textSrc, err := fs.ReadFile(content, textName)
if err != nil {
return "", "", fmt.Errorf("mailer: read %s: %w", textName, err)
}
htmlSrc, err := fs.ReadFile(content, htmlName)
if err != nil {
return "", "", fmt.Errorf("mailer: read %s: %w", htmlName, err)
}
textTpl := texttemplate.Must(layoutText.Clone())
if _, err := textTpl.New("content").Parse(string(textSrc)); err != nil {
return "", "", fmt.Errorf("mailer: parse %s: %w", textName, err)
}
htmlTpl := htmltemplate.Must(layoutHTML.Clone())
if _, err := htmlTpl.New("content").Parse(string(htmlSrc)); err != nil {
return "", "", fmt.Errorf("mailer: parse %s: %w", htmlName, err)
}
lv := layoutVars{Content: vars, Frame: frame}
var textBuf, htmlBuf bytes.Buffer
if err := textTpl.ExecuteTemplate(&textBuf, "layout.txt", lv); err != nil {
return "", "", fmt.Errorf("mailer: render %s: %w", textName, err)
}
if err := htmlTpl.ExecuteTemplate(&htmlBuf, "layout.html", lv); err != nil {
return "", "", fmt.Errorf("mailer: render %s: %w", htmlName, err)
}
return textBuf.String(), htmlBuf.String(), nil
}
+65
View File
@@ -0,0 +1,65 @@
package mailer
import (
"strings"
"testing"
"testing/fstest"
)
// Every email Minstrel sends goes out in the one layout: the Minstrel name,
// its heading on the card, and a footer naming the sending server and why.
func TestEveryEmailIsFramedByTheLayout(t *testing.T) {
reset := func() (string, string, error) {
return RenderResetEmail(ResetEmailVars{
Username: "alice", ResetURL: "https://music.example/reset/abc", ExpiryHours: 1, Server: "https://music.example",
})
}
smtpTest := func() (string, string, error) { return RenderSMTPTestEmail("https://music.example/") }
for name, render := range map[string]func() (string, string, error){"reset": reset, "smtp test": smtpTest} {
text, html, err := render()
if err != nil {
t.Fatalf("%s: %v", name, err)
}
for _, want := range []string{
`">Minstrel</td>`,
"<h1 style=",
"Sent by the Minstrel server at https://music.example because ",
"— Minstrel",
} {
if !strings.Contains(html, want) {
t.Errorf("%s html is missing %q", name, want)
}
}
if !strings.Contains(text, "Sent by the Minstrel server at https://music.example because ") ||
!strings.HasSuffix(strings.TrimSpace(text), "— Minstrel") {
t.Errorf("%s text lacks the layout's footer:\n%s", name, text)
}
}
}
func TestFrameWithoutAPublicAddress(t *testing.T) {
content := fstest.MapFS{"c.txt": {Data: []byte("Hi.")}, "c.html": {Data: []byte("<p>Hi.</p>")}}
text, html, err := Render(content, "c.txt", "c.html", nil, Frame{
Heading: "Hello", Reason: "you asked", NotificationSettings: true,
})
if err != nil {
t.Fatal(err)
}
for _, body := range []string{text, html} {
if !strings.Contains(body, "Sent by your Minstrel server because you asked.") {
t.Errorf("footer should name the server generically:\n%s", body)
}
if !strings.Contains(body, "under Settings → Notifications") || strings.Contains(body, "http") {
t.Errorf("with no address, the settings line says where to look, without a link:\n%s", body)
}
}
}
// A misspelt style name fails the render rather than shipping an unstyled email.
func TestUnknownStyleFailsTheRender(t *testing.T) {
content := fstest.MapFS{"c.txt": {Data: []byte("x")}, "c.html": {Data: []byte(`<p style="{{style "lnik"}}">x</p>`)}}
if _, _, err := Render(content, "c.txt", "c.html", nil, Frame{Heading: "h", Reason: "r"}); err == nil {
t.Fatal("want an error for an unknown style")
}
}
+30 -31
View File
@@ -1,45 +1,44 @@
package mailer
import (
"bytes"
"embed"
"fmt"
htmltemplate "html/template"
texttemplate "text/template"
)
import "embed"
//go:embed templates/password_reset.txt templates/password_reset.html
var resetEmailFS embed.FS
//go:embed templates/password_reset.txt templates/password_reset.html templates/smtp_test.txt templates/smtp_test.html
var emailsFS embed.FS
// ResetEmailVars is the data passed to the password reset templates.
type ResetEmailVars struct {
Username string
ResetURL string
ExpiryHours int
}
// RenderResetEmail returns (textBody, htmlBody) for the password
// reset email, parameterized with the given vars.
func RenderResetEmail(vars ResetEmailVars) (string, string, error) {
textTpl, err := texttemplate.ParseFS(resetEmailFS, "templates/password_reset.txt")
if err != nil {
return "", "", fmt.Errorf("mailer: parse text template: %w", err)
}
htmlTpl, err := htmltemplate.ParseFS(resetEmailFS, "templates/password_reset.html")
if err != nil {
return "", "", fmt.Errorf("mailer: parse html template: %w", err)
}
var textBuf, htmlBuf bytes.Buffer
if err := textTpl.Execute(&textBuf, vars); err != nil {
return "", "", fmt.Errorf("mailer: render text: %w", err)
}
if err := htmlTpl.Execute(&htmlBuf, vars); err != nil {
return "", "", fmt.Errorf("mailer: render html: %w", err)
}
return textBuf.String(), htmlBuf.String(), nil
// Server is the server's public address, named in the footer as the
// sender.
Server string
}
// ResetEmailSubject is the subject line used by the forgot-password
// flow. Exported so callers don't string-literal it independently.
const ResetEmailSubject = "Minstrel password reset"
// RenderResetEmail returns (textBody, htmlBody) for the password reset
// email.
func RenderResetEmail(vars ResetEmailVars) (string, string, error) {
return Render(emailsFS, "templates/password_reset.txt", "templates/password_reset.html", vars, Frame{
Heading: "Reset your password",
Server: vars.Server,
Reason: "a password reset was requested for your account",
})
}
// SMTPTestSubject is the subject of the message an admin sends to check
// the server's email settings.
const SMTPTestSubject = "Minstrel email test"
// RenderSMTPTestEmail returns (textBody, htmlBody) for the email settings
// test message. server is the server's public address, or "".
func RenderSMTPTestEmail(server string) (string, string, error) {
return Render(emailsFS, "templates/smtp_test.txt", "templates/smtp_test.html", struct{}{}, Frame{
Heading: "Email is working",
Server: server,
Reason: "an admin sent a test from the email settings",
})
}
+29
View File
@@ -0,0 +1,29 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<meta name="supported-color-schemes" content="dark">
</head>
<body style="{{style "page"}}">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="{{style "wrap"}}">
<tr><td align="center" style="padding:32px 16px;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:560px;">
<tr><td style="{{style "brand"}}">Minstrel</td></tr>
<tr><td style="{{style "card"}}">
<h1 style="{{style "heading"}}">{{.Frame.Heading}}</h1>
{{template "content" .Content}}
</td></tr>
<tr><td style="{{style "footer"}}">
Sent by {{.Frame.ServerName}} because {{.Frame.Reason}}.<br>
{{- if .Frame.NotificationSettings}}
{{with .Frame.SettingsURL}}<a href="{{.}}" style="{{style "faint"}}">Choose which notifications are emailed to you</a>{{else}}Choose which notifications are emailed to you in Minstrel, under Settings → Notifications.{{end}}<br>
{{- end}}
— Minstrel
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
{{.Frame.Heading}}
{{template "content" .Content}}
--
Sent by {{.Frame.ServerName}} because {{.Frame.Reason}}.
{{- if .Frame.NotificationSettings}}
{{with .Frame.SettingsURL}}Choose which notifications are emailed to you: {{.}}{{else}}Choose which notifications are emailed to you in Minstrel, under Settings → Notifications.{{end}}
{{- end}}
— Minstrel
+7 -29
View File
@@ -1,29 +1,7 @@
<!doctype html>
<html>
<body style="font-family: -apple-system, system-ui, sans-serif; max-width: 560px; margin: 0 auto; padding: 24px; color: #2c2c2c;">
<p>Hello <strong>{{.Username}}</strong>,</p>
<p>A password reset was requested for your Minstrel account. To set a new
password, click the button below within <strong>{{.ExpiryHours}}</strong> hours:</p>
<p style="margin: 24px 0;">
<a href="{{.ResetURL}}" style="display: inline-block; padding: 12px 24px;
background: #4a6b5c; color: #ffffff; text-decoration: none; border-radius: 6px;
font-weight: 500;">Reset password</a>
</p>
<p style="font-size: 14px; color: #666;">
Or copy this URL into your browser:<br>
<code style="word-break: break-all;">{{.ResetURL}}</code>
</p>
<p style="font-size: 14px; color: #666;">
If you didn't request this, you can safely ignore this email — your
password won't change.
</p>
<p style="font-size: 12px; color: #999; margin-top: 32px; border-top: 1px solid #eee; padding-top: 16px;">
— Minstrel
</p>
</body>
</html>
<p style="{{style "p"}}">Hello <strong>{{.Username}}</strong>,</p>
<p style="{{style "p"}}">A password reset was requested for your Minstrel account. To set a new
password, use the button below within <strong>{{.ExpiryHours}}</strong> hours.</p>
<p style="margin:24px 0;"><a href="{{.ResetURL}}" style="{{style "button"}}">Reset password</a></p>
<p style="{{style "p"}} {{style "quiet"}}">Or copy this address into your browser:<br>
<span style="{{style "code"}}">{{.ResetURL}}</span></p>
<p style="margin:0; {{style "quiet"}}">If you didn't ask for this, you can ignore this email. Your password won't change.</p>
+2 -4
View File
@@ -5,7 +5,5 @@ password, follow this link within {{.ExpiryHours}} hours:
{{.ResetURL}}
If you didn't request this, you can safely ignore this email — your
password won't change.
— Minstrel
If you didn't ask for this, you can ignore this email. Your password
won't change.
+2
View File
@@ -0,0 +1,2 @@
<p style="{{style "p"}}">This is a test message from your Minstrel server.</p>
<p style="margin:0;">It arrived, so the server's email settings work: password resets and the notification emails people choose will reach their inboxes.</p>
+4
View File
@@ -0,0 +1,4 @@
This is a test message from your Minstrel server.
It arrived, so the server's email settings work: password resets and the
notification emails people choose will reach their inboxes.