feat(postcard): mail branding and the logo URL

- Brand, DefaultBrand and CompileBrandCSS: thirteen colours, each checked
  as a six-digit hex colour, compiled into CSS for plain elements and the
  built-in layouts and partials
- once the branding row exists its compiled CSS replaces mail.brandCss;
  mail.css still follows, and the file version sent after a failing
  override keeps the Branding
- layouts get logoUrl, built per send from storage.uploads.public_url or
  app.url through attach.AbsolutePublicURL; a logo that cannot be shown is
  left out with a rate-limited warning
- Editor previews and validates a Branding draft (rule colour)

Plan 35-01, task 5.
This commit is contained in:
Jakub Zych
2026-11-13 11:06:43 +01:00
parent 935e4bf64f
commit 2bf9081c1b
10 changed files with 974 additions and 27 deletions

View File

@@ -101,11 +101,13 @@ func (m *mailer) Send(ctx context.Context, msg Message) error {
// overrides, or overrides that fail to render or to pass the safety checks,
// it renders the file version.
func (m *mailer) render(ctx context.Context, msg Message) (RenderedMessage, error) {
file := renderOptions{css: m.opts.CSS, brandCSS: m.opts.BrandCSS}
if _, err := m.catalog.lookupTemplate(msg.Template); err != nil {
return RenderedMessage{}, err
}
ov := m.overrides(ctx, msg.Template)
// The file version carries the Branding too: it is not an override of a
// file, and a failing override must not change how the mail looks.
file := m.options(ov.Brand)
if ov.empty() {
return m.catalog.render(msg, file)
}
@@ -130,6 +132,26 @@ func (m *mailer) render(ctx context.Context, msg Message) (RenderedMessage, erro
return rendered, nil
}
// options returns the render options of the file version: mail.css, and
// either the saved Branding (its compiled CSS and its logo) or, while none
// has been saved, mail.brandCss. A Branding whose colours do not compile
// keeps mail.brandCss.
func (m *mailer) options(brand *Brand) renderOptions {
opts := renderOptions{css: m.opts.CSS, brandCSS: m.opts.BrandCSS}
if brand == nil {
return opts
}
opts.logoURL = brand.LogoURL
css, err := CompileBrandCSS(*brand)
if err != nil {
m.warn(err.Error(), "postcard: mail branding is not valid, using mail.brandCss",
slog.String("error", err.Error()))
return opts
}
opts.brandCSS = css
return opts
}
// overrides asks the source once. A failed lookup (no connection, tables not
// migrated yet) is a warning, not a send failure: the file version is sent.
func (m *mailer) overrides(ctx context.Context, template string) Overrides {
@@ -144,6 +166,9 @@ func (m *mailer) overrides(ctx context.Context, template string) Overrides {
)
return Overrides{}
}
for _, warning := range ov.warnings {
m.warn(warning, "postcard: "+warning, slog.String("template", template))
}
return ov
}