Sending

Profiles and config

Define sender profiles, fill a Config, and let NewRenderer check everything at once — then route a rendered Message yourself.

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

profile.go
type Profile struct {
    Name            string
    From            mail.Address
    FromDomain      string
    Headers         map[string]string
    ListUnsubscribe bool
}
FieldRuleMeaning
Namenon-empty, unique in Config.ProfilesWhat a template's frontmatter profile: names. Copied to Message.Profile, so your application can route on it.
Fromzero, or a valid addressThe fixed sender. Zero means every template of this profile must set frontmatter from instead.
FromDomaina hostname, or emptyThe domain a from (frontmatter or the fixed From) must be on, compared case-insensitively.
Headerseach value must pass Message.SetHeader's rulesAdded 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 frontmatter from out — setting it fails to render with frontmatter: "from" is not allowed: profile "orders" has a fixed From.
  • From the template: leave From zero. Every template of the profile must then set from:, or render fails with frontmatter: profile "b2b" has no From, so "from" is required. The evaluated address is parsed, checked against FromDomain when one is set (from "x@elsewhere.com" is not on partners.acme.example), and checked with the same address rule a fixed From gets: a valid local@domain address, 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

config.go
type Config struct {
    Kit               *kit.Kit
    Profiles          []Profile
    AssetBase         string
    Vars              map[string]string
    AllowRelativeURLs bool
}
FieldRuleUsed for
Kitnot nilEvery template must be compiled against this same *kit.Kit.
Profilesat least one; every name non-empty and uniqueThe sender profiles a template's frontmatter profile: can name.
AssetBasean 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.
Varskeys must be kit vars other than asset_base; values must hold no <, > or control characterOverrides 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:

plain/kit.yaml#L46-L51
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: is vars: unknown var "x"; a key of asset_base is vars: "asset_base" is set with AssetBase.
  • The value has an unsafe character. <, >, or any control character (including DEL) fails with vars: "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

renderer.go
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:

main.go
_, 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"},
})
Error
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.

renderer.go
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.

main.go
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:

main.go
// 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 with Config.Kit.
Copyright © 2026