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:
@@ -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",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user