package mailer import ( "bytes" "embed" "fmt" htmltemplate "html/template" "io/fs" "sort" "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, so an email reads as part of the same product. An email is // dark, the app's own look, wherever the client can't say which the reader // prefers; a client that says they use a light theme shows the design // system's light colours instead (lightTheme). //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, "class": class, "lightTheme": lightTheme, }). 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 // The light theme's values of the same tokens. The action colour and the // text on it are one value in both themes. lightPage = "#F5F1E8" // --fs-surface-page lightCard = "#FBF8F0" // --fs-surface-raised lightBorder = "#D9D6CE" // --fs-border-color lightText = "#14171A" // --fs-text-primary lightSecondary = "#5A5852" // --fs-text-secondary lightTertiary = "#9A9890" // --fs-text-tertiary lightLink = "#2C3D38" // --fs-accent-fg, mixed with the light text colour 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 + ";", } // lightStyles are the colours each style takes in a light theme. A style // with a colour has an entry, and an element using it carries its class, // written `class="{{class "link"}}"`, for the light colours to reach it. // Inline styles can't follow the reader's theme, so the light colours are // a stylesheet that overrides them; a client that drops the stylesheet or // can't tell the theme keeps the inline dark colours. var lightStyles = map[string]string{ "page": "background:" + lightPage, "wrap": "background:" + lightPage, "brand": "color:" + lightText, "heading": "color:" + lightText, "card": "background:" + lightCard + "; border-color:" + lightBorder + "; color:" + lightText, "footer": "color:" + lightTertiary, "link": "color:" + lightLink, "quiet": "color:" + lightSecondary, "faint": "color:" + lightTertiary, "group": "color:" + lightText, "code": "color:" + lightSecondary, } // 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 } // class returns the class names that give the named styles their light // colours. An unknown name, or one with no light colours, fails the render. func class(names ...string) (string, error) { out := make([]string, 0, len(names)) for _, n := range names { if _, ok := lightStyles[n]; !ok { return "", fmt.Errorf("mailer: no light colours for email style %q", n) } out = append(out, "m-"+n) } return strings.Join(out, " "), nil } // lightTheme is the stylesheet that gives each class its light colours when // the reader's system uses a light theme. func lightTheme() htmltemplate.CSS { names := make([]string, 0, len(lightStyles)) for n := range lightStyles { names = append(names, n) } sort.Strings(names) var b strings.Builder b.WriteString("@media (prefers-color-scheme: light) {\n") for _, n := range names { decls := strings.Split(lightStyles[n], "; ") for i, d := range decls { decls[i] = d + " !important" } fmt.Fprintf(&b, ".m-%s { %s; }\n", n, strings.Join(decls, "; ")) } b.WriteString("}") return htmltemplate.CSS(b.String()) //nolint:gosec // built from the fixed 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 }