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>
148 lines
6.3 KiB
Go
148 lines
6.3 KiB
Go
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
|
|
}
|