package templates
import (
"bytes"
"html/template"
"strings"
"github.com/warmbly/warmbly/internal/config"
"github.com/warmbly/warmbly/internal/observability/errs"
)
// ─── Centralized Business Details ────────────────────────────────
// Branding and legal info for every email template, resolved from
// config.Brand() so the footer, the sign-in screen and a public form page all
// say the same thing about who this deployment is.
//
// These are functions, not constants, because a self-hosted install must not
// send mail attributed to Mindroot Ltd with links to someone else's dashboard.
// On a self-host the hosted defaults are not a fallback at all: they are
// another company's registered details and another company's website. Unset
// means unset there, and the footer drops the row rather than filling it with
// ours.
// CompanyName is the product name in subjects and the header. Unlike the legal
// details it is true of a self-host too: it is the software's name.
func CompanyName() string { return config.Brand().Name }
// AppURL is the dashboard base every emailed link is built from.
func AppURL() string { return config.AppBaseURL() }
// footerLink is one entry in the footer's link row. It is a list rather than
// three fixed slots so an install that configured none of them renders no row
// instead of three links to nowhere.
type footerLink struct {
Label string
URL string
}
type baseData struct {
Subject string
Content template.HTML
CompanyName string
FooterLinks []footerLink
LegalLine string
RegisteredAddr string
}
// footerLinks is the configured subset of privacy, terms and website.
func footerLinks(b config.BrandConfig) []footerLink {
var links []footerLink
for _, l := range []footerLink{
{Label: "Privacy", URL: b.PrivacyURL},
{Label: "Terms", URL: b.TermsURL},
{Label: b.WebsiteLabel(), URL: b.WebsiteURL},
} {
if l.URL != "" && l.Label != "" {
links = append(links, l)
}
}
return links
}
// legalLine is the Companies Act 2006 identification line, which only the
// hosted service is the subject of. A self-host renders no line rather than
// naming a company that has nothing to do with the mail it just sent.
func legalLine(b config.BrandConfig) string {
parts := []string{}
for _, p := range []string{b.LegalEntity, b.CompanyNumber, b.PlaceOfReg} {
if p = strings.TrimSpace(p); p != "" {
parts = append(parts, p)
}
}
if len(parts) == 0 {
return ""
}
return "\u00a9 " + strings.Join(parts, " \u00b7 ")
}
var baseTmpl = template.Must(template.New("base").Parse(baseHTML))
func renderEmail(subject, content string) (string, error) {
// One read of the environment per render, so the header, the links and
// the legal line can never disagree with each other.
brand := config.Brand()
data := baseData{
Subject: subject,
Content: template.HTML(content),
CompanyName: brand.Name,
FooterLinks: footerLinks(brand),
LegalLine: legalLine(brand),
RegisteredAddr: brand.Address,
}
var buf bytes.Buffer
if err := baseTmpl.Execute(&buf, data); err != nil {
errs.CaptureException(err)
return "", err
}
return buf.String(), nil
}
// Dashboard-style transactional email shell.
//
// Replaces the previous radial-blue-gradient marketing-y design with
// the same chrome the user sees in the dashboard:
// - clean cream background (#f5f6f8),
// - white card with a hairline #e2e8f0 border,
// - slate type, no fancy gradients,
// - slate-900 logo monogram, no decorative haze.
const baseHTML = `
{{.Subject}}
|
|
|
|
{{if .FooterLinks}}
|
{{range $i, $link := .FooterLinks}}{{if $i}} ·
{{end}}{{$link.Label}}
{{end}} |
{{end}}
{{if .LegalLine}}
|
{{.LegalLine}}
|
{{end}}
{{if .RegisteredAddr}}
|
{{.RegisteredAddr}}
|
{{end}}
|
`