Quick start
This page builds one email end to end with Plain, lttr's small starter kit: a data type, a Markdown template, a profile, a render. Everything fits in one file, which you can copy, run, and then take apart section by section.
Run the program
Create a module and add lttr (see Installation for private module access):
mkdir quickstart && cd quickstart
go mod init example.com/quickstart
go get github.com/sulv-io/lttr@latest
Save this as main.go:
package main
import (
"fmt"
"log"
"net/mail"
"github.com/sulv-io/lttr"
"github.com/sulv-io/lttr/kits/plain"
)
// Welcome is the template's data. Every merge tag in the template is
// checked against this type when the template compiles.
type Welcome struct {
FirstName string `json:"first_name"`
Workspace string `json:"workspace"`
SetupURL string `json:"setup_url"`
}
// The template: Markdown plus the kit's components. In a real service it
// usually lives in a .md file pulled in with //go:embed.
const welcomeSrc = `---
template: welcome
profile: default
subject: "Welcome to Acme, {{first_name}}"
preheader: "Your workspace is ready."
---
# Welcome, {{first_name}}
Your workspace **{{workspace}}** is ready. Invite your team, then pick a plan.
::button{href="{{setup_url}}" label="Finish setting up"}
::
::footer{note="Questions? Reply to this email."}
::
`
// Compile once, at init, against the kit: a template error panics at
// startup with name:line:col, never at send time.
var welcome = lttr.MustCompile[Welcome](plain.MustKit(), "welcome", []byte(welcomeSrc))
func main() {
r, err := lttr.NewRenderer(lttr.Config{
Kit: plain.MustKit(), // the kit the template was compiled against
Profiles: []lttr.Profile{{
Name: "default",
From: mail.Address{Name: "Acme", Address: "hello@acme.example"},
}},
Vars: map[string]string{"brand": "Acme", "company": "Acme Inc.", "address": "1 Harbour Street, Springfield"},
})
if err != nil {
log.Fatal(err) // every config problem at once
}
msg, err := welcome.Render(r,
lttr.Recipient{Address: "ada@example.com", Name: "Ada"},
Welcome{FirstName: "Ada", Workspace: "Northwind Studio", SetupURL: "https://acme.example/setup"})
if err != nil {
log.Fatal(err)
}
fmt.Println(msg.Subject)
fmt.Println()
fmt.Println(msg.Text)
}
Run it:
go run .
Welcome to Acme, Ada
Acme
WELCOME, ADA
Your workspace Northwind Studio is ready. Invite your team, then pick
a plan.
Finish setting up:
https://acme.example/setup
--
Questions? Reply to this email.
Acme Inc. · 1 Harbour Street, Springfield
That's msg.Text, the plain-text part. msg.HTML is the other half: Outlook-safe, table-based markup with the same content. Misspell a merge tag — {{workspce}} for {{workspace}} — and the program never gets this far:
panic: welcome:10:18: unknown name "workspce" (did you mean workspace?)
The position is the line and column in the template source, because MustCompile runs before main. When a known name is close to the one written, the error ends with it as a suggestion.
Define the data
Each template has a Go data type, and every merge tag in the template must resolve to a field of it.
type Welcome struct {
FirstName string `json:"first_name"`
Workspace string `json:"workspace"`
SetupURL string `json:"setup_url"`
}
A merge tag matches a field by its JSON name, or by its lower-cased Go name when there is no json tag, so {{first_name}} is FirstName. A tag must resolve to a scalar: a string or integer kind, or any fmt.Stringer (a kit's Money type, say). Names your type does not have are looked up on the Recipient next — Welcome has no name, so a bare {{name}} would be the recipient's name. See Merge tags and data for paths, fallbacks and untyped data.
Write the template
The template is Markdown with YAML frontmatter and the kit's components. The program keeps it in a string constant; in a service it usually lives in a .md file embedded with //go:embed.
---
template: welcome
profile: default
subject: "Welcome to Acme, {{first_name}}"
preheader: "Your workspace is ready."
---
# Welcome, {{first_name}}
Your workspace **{{workspace}}** is ready. Invite your team, then pick a plan.
::button{href="{{setup_url}}" label="Finish setting up"}
::
::footer{note="Questions? Reply to this email."}
::
- The frontmatter names the template (it must equal the name passed to
Compile) and the profile it sends under, and holds the subject and the preheader, the preview line an inbox shows after the subject. ::buttonand::footerare two of Plain's six components (section,button,image,columns— ofcolumn— andfooter); loose Markdown, like the heading and paragraph here, goes intosection, Plain's default.- Every attribute and merge tag is checked against Plain's
component.yamland againstWelcomewhen the template compiles.
Build the renderer
Config is everything a Renderer needs: the kit the template was compiled against, the profiles it may name, and any var overrides.
r, err := lttr.NewRenderer(lttr.Config{
Kit: plain.MustKit(),
Profiles: []lttr.Profile{{
Name: "default",
From: mail.Address{Name: "Acme", Address: "hello@acme.example"},
}},
Vars: map[string]string{"brand": "Acme", "company": "Acme Inc.", "address": "1 Harbour Street, Springfield"},
})
NewRenderer validates all of it and reports every problem at once: a duplicate or unnamed profile, a From address that doesn't parse, an asset base that isn't https, a Vars key the kit doesn't declare. Vars here overrides three of Plain's own defaults (brand, company, address); leave any of them out and the kit's own value is used. A Renderer is immutable and safe for concurrent use, so build one and share it. See Profiles and config.
Render
Render takes the renderer, the recipient and the data, and returns a *lttr.Message or an error, never a partial message:
msg, err := welcome.Render(r,
lttr.Recipient{Address: "ada@example.com", Name: "Ada"},
Welcome{FirstName: "Ada", Workspace: "Northwind Studio", SetupURL: "https://acme.example/setup"})
It checks everything that depends on data: a merge tag with no value and no fallback, an invalid URL, an empty subject, HTML of 100 KB or more (Gmail clips at 102 KB). The message carries the envelope (Subject, From, To), Profile (so your application can route on it), and both bodies (HTML, Text). Sending it is yours: <tr.SMTP{Addr: "localhost:1025"} sends over STARTTLS to a relay or a local catcher, and msg.MIME(time.Now()) gives you the raw bytes if you deliver another way. See SMTP.
Next steps
Once the shape is familiar, start a kit of your own instead of using Plain directly:
lttr kit init ./mykit
wrote 29 files to ./mykit; check it with: lttr kit check ./mykit
kit init copies Plain's files into ./mykit, named after the directory. Edit the tokens, layout and components, then check it — kit check loads the kit, renders every samples/*.md and lints the HTML:
lttr kit check ./mykit
kit mykit: 2 samples rendered, no problems
Then watch it as you edit:
lttr serve -kit ./mykit
serve runs the studio: it watches the whole kit (kit.yaml, templates, styles, samples), re-renders what a save changed, and shows each sample at 600 and 375 px, in light and dark, with its text part and its problems. See What is a kit and Studio.
| If you want to | Read |
|---|---|
| Learn what a template can contain | Comark syntax |
| Build or edit a kit | What is a kit |
| Understand every compile error | Compile errors |
| See nine complete emails on Quiet | The Quiet kit |
| Send marketing, security and internal mail | Profiles and config |