Profiles and config
Rendering takes two pieces you fill in: one or more Profiles, which say who a message is from and what it carries, and a Config that ties them to a kit. NewRenderer checks all of it and hands back an immutable *Renderer — or every problem at once, before any template runs.
Define a profile
type Profile struct {
Name string
From mail.Address
FromDomain string
Headers map[string]string
ListUnsubscribe bool
}
| Field | Rule | Meaning |
|---|---|---|
Name | non-empty, unique in Config.Profiles | What a template's frontmatter profile: names. Copied to Message.Profile, so your application can route on it. |
From | zero, or a valid address | The fixed sender. Zero means every template of this profile must set frontmatter from instead. |
FromDomain | a hostname, or empty | The domain a from (frontmatter or the fixed From) must be on, compared case-insensitively. |
Headers | each value must pass Message.SetHeader's rules | Added to every message the profile produces, e.g. X-SES-CONFIGURATION-SET. See Set a custom header. |
ListUnsubscribe | — | Sets the RFC 8058 one-click headers from the recipient's UnsubscribeURL, which must then be an absolute https URL. |
A profile carries no sending policy of its own — no stream, no suppression list. Routing and suppression are your application's job; see Route on Message.Profile below.
Choose where From comes from
A profile either fixes its sender or takes it from the template:
- Fixed: set
From. Every template of the profile must then leave frontmatterfromout — setting it fails to render withfrontmatter: "from" is not allowed: profile "orders" has a fixed From. - From the template: leave
Fromzero. Every template of the profile must then setfrom:, or render fails withfrontmatter: profile "b2b" has no From, so "from" is required. The evaluated address is parsed, checked againstFromDomainwhen one is set (from "x@elsewhere.com" is not on partners.acme.example), and checked with the same address rule a fixedFromgets: a validlocal@domainaddress, and a name with no CR, LF or NUL.
FromDomain applies to either path: a fixed From off its domain fails when the config is checked (config: profile "orders": from "orders@other.example" is not on acme.example); a frontmatter from off it fails at render.
Fill the config
type Config struct {
Kit *kit.Kit
Profiles []Profile
AssetBase string
Vars map[string]string
AllowRelativeURLs bool
}
| Field | Rule | Used for |
|---|---|---|
Kit | not nil | Every template must be compiled against this same *kit.Kit. |
Profiles | at least one; every name non-empty and unique | The sender profiles a template's frontmatter profile: can name. |
AssetBase | an absolute https URL, or relative with AllowRelativeURLs; empty keeps the kit's own asset_base default | {{var "asset_base"}} in kit templates — the base URL logos and images resolve against. |
Vars | keys must be kit vars other than asset_base; values must hold no <, > or control character | Overrides the kit's own var defaults per renderer — the brand name, a footer address, and so on. |
AllowRelativeURLs | — | Lets a relative AssetBase, and relative URLs anywhere in a template, through. Previews only: a relative URL means nothing in an inbox. |
There is no DefaultConfig: every value is yours.
Let the kit set the asset base
AssetBase is where your logo and image files are served from. When you leave it empty, NewRenderer falls back to the kit's own asset_base var default — and that default must pass the same https rule. The Plain kit ships one:
vars:
brand: Example
site: https://example.com
company: Example Inc.
address: 100 Main Street, Springfield
asset_base: https://example.com/assets
so lttr.Config{Kit: plain.MustKit()} — no AssetBase at all — renders with {{var "asset_base"}} equal to https://example.com/assets. A kit with no asset_base default, and a Config with no AssetBase, leaves the var empty: fine for a kit whose components never link an image. Setting Config.AssetBase always wins over the kit's default.
Override kit vars, safely
Vars overrides the kit's own var defaults — everything under vars: in kit.yaml except asset_base, which AssetBase sets instead. Two things make a var override fail:
- The name isn't the kit's. A key the kit does not declare under
vars:isvars: unknown var "x"; a key ofasset_baseisvars: "asset_base" is set with AssetBase. - The value has an unsafe character.
<,>, or any control character (including DEL) fails withvars: "brand": unsafe character '<'— vars are written straight into kit templates, so this is what stops a var value from injecting markup.
Let NewRenderer report every problem
func NewRenderer(cfg Config) (*Renderer, error)
NewRenderer checks the whole config — every profile, the asset base, every var, and the kit's own templates against those vars — and joins every problem it finds, rather than stopping at the first. For this config:
_, err := lttr.NewRenderer(lttr.Config{
Kit: plain.MustKit(),
Profiles: []lttr.Profile{
{Name: "", From: mail.Address{Name: "No Address"}},
{Name: "orders", From: mail.Address{Address: "orders@acme.example"}, FromDomain: "not a hostname"},
{Name: "orders", From: mail.Address{Address: "orders@acme.example"}},
},
AssetBase: "not-https",
Vars: map[string]string{"nope": "x", "brand": "Ac<me"},
})
config: Profiles[0] has no name
config: profile "": from has a name but no address
config: profile "orders": FromDomain "not a hostname" is not a hostname
config: profile "orders" is defined twice
vars: "brand": unsafe character '<'
vars: unknown var "nope"
config: AssetBase "not-https" must be an absolute https URL
(captured from a real run against this checkout). Every profile-specific error names the profile (profile "orders", Profiles[0]), so it's easy to trace back to the lttr.Profile{...} in Config.Profiles that caused it.
A *Renderer is immutable and safe for concurrent use; it keeps its own copy of every profile. Build one at start-up and share it.
func (r *Renderer) Kit() *kit.Kit
func (r *Renderer) Profile(name string) (Profile, bool)
func (r *Renderer) Profiles() []Profile
Profile and Profiles return copies — changing one never changes the renderer's own.
Route on Message.Profile
lttr does no routing of its own: Message.Profile is the profile name Render copied from the template's frontmatter, and routing which Sender a message goes through is entirely your application's decision — a plain switch, a map lookup, or whatever your service already uses for that kind of choice.
package main
import (
"context"
"fmt"
"github.com/sulv-io/lttr"
)
// send routes m to a Sender by the profile its template named: transactional
// profiles share one sender, marketing profiles (which set ListUnsubscribe)
// share another, and internal mail goes through a third.
func send(ctx context.Context, senders map[string]lttr.Sender, m *lttr.Message) error {
switch m.Profile {
case "buyer", "seller", "driver", "security":
return senders["transactional"].Send(ctx, m)
case "announcement", "newsletter", "promo":
return senders["marketing"].Send(ctx, m)
case "internal":
return senders["internal"].Send(ctx, m)
default:
return fmt.Errorf("no sender for profile %q", m.Profile)
}
}
func main() {}
Sender is one method, so a suppression check or an internal-recipient allow-list is just a wrapper around it:
// suppressing skips a send when m.Profile is on the suppression list for m.To.
type suppressing struct {
next lttr.Sender
isSuppressed func(profile, addr string) bool
}
func (s suppressing) Send(ctx context.Context, m *lttr.Message) error {
if s.isSuppressed(m.Profile, m.To.Address) {
return fmt.Errorf("%s: recipient suppressed for %q", m.To.Address, m.Profile)
}
return s.next.Send(ctx, m)
}
See SMTP for the one Sender lttr ships, and Message and MIME for everything else on *Message.
Next steps
- Message and MIME for the rest of
*Message, custom headers and the size limit. - SMTP for delivering the message.
- The lttr CLI for
-kit, the flag every command shares withConfig.Kit.
Plain text
How a kit writes the plain-text part — text templates, the core block forms and blocks.txt, separators, the wrapping and table helpers, and how the parts are joined.
Message and MIME
The rendered email as a Go value, the rules for adding your own headers, the exact bytes MIME writes, and the size limit Render enforces.