Email-safe HTML
A kit owns every tag of its HTML, so it owns the job of writing markup mail clients render: tables for layout, inline styles, images with explicit sizes, and Outlook's conditional comments. lttr helps in three ways. The core functions write the fiddly parts, html/template escapes every value, and a structural lint checks the rendered result.
Lint the rendered HTML
lttr kit check renders every sample of a kit and lints the HTML. lttr lint -kit ./emailkit file.md lints the HTML of a file only when its template: names one of the kit's samples: it compiles the file like that sample, renders it with the sample's data and lints the result. Any other file is compiled as untyped data only, with no render and no HTML lint. Both exit 1 on any problem, and print compile warnings without failing. The lint reports:
| Rule | Output from a copy of Plain that breaks it |
|---|---|
Every <table> has role="presentation" cellpadding="0" cellspacing="0" border="0" | html: <table> at byte 8151: missing role="presentation" |
Every <img> has alt, width and border:0 in its style | html: <img> at byte 7066: missing alt |
No flex or grid in a style attribute or <style> element | html: css contains "flex" at byte 5348 |
No <div> styled display:flex | html: <div> at byte 5540: style has display:flex |
No var(, @import, <script or {{ anywhere | html: contains "var(" at byte 5400 |
No ZgotmplZ: a value html/template rejected | html: contains "ZgotmplZ" at byte 10556 (a value html/template rejected) |
| Outlook conditional comments open and close in order | html: mso conditional closed before it was opened at byte 10631 |
| Outlook conditional openers and closers balance | html: mso conditionals unbalanced: 2 openers, 3 closers |
A kit with dark: true declares its colour schemes | html: missing <meta name="color-scheme" content="light dark"> |
| The document is under 100 KB | Rendering stops first: render: html is 110700 bytes; gmail clips at 102 KB |
In the output, each line starts with the sample's path: emailkit/samples/welcome.md: html: …. Markup inside Outlook-only comments is linted too: its tables and images follow the same rules.
The lint is the public package github.com/sulv-io/lttr/lint. lint.Lint(html, lint.Options{Dark: k.Dark()}) returns one message per violation for a rendered document, and lint.Check(set) compiles, renders and lints every sample of an lttr.SampleSet. For a kit the CLI can load, lttr kit check runs it for you. For a kit that binds Go types, see Check a typed kit from Go.
Rendering itself fails at 100 KB of HTML and adds a warning to the message above 90 KB, because Gmail clips messages at 102 KB.
Build with presentation tables
Plain's layout is tables all the way down: the canvas, an Outlook-only fixed-width table, and the surface that holds one row per component.
<body id="body" class="pl-canvas" style="margin:0;padding:0;{{style "background-color" "color.canvas"}}">
{{preheader .Preheader}}
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="{{token "color.canvas"}}" class="pl-canvas"><tr><td class="pl-outer" align="center" valign="top" style="padding:24px 12px;">
{{/* Outlook ignores max-width, so it gets a fixed-width table. This is
what msoOpen writes, spelled out because msoOpen takes an int and tokens
are strings. */ -}}
{{msoIf}}<table role="presentation" width="{{token "size.width"}}" align="center" cellpadding="0" cellspacing="0" border="0"><tr><td>{{msoEnd}}
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="{{token "color.surface"}}" class="pl-surface" style="max-width:{{token "size.width"}}px;{{style "background-color" "color.surface"}}{{style "border" (printf "1px solid %s" (token "color.border"))}}">
<tr><td class="pl-px" align="left" style="padding:24px {{token "size.side"}}px;">
<table role="presentation" cellpadding="0" cellspacing="0" border="0"><tr>
<td valign="middle" style="padding:0 10px 0 0;"><a href="{{var "site"}}"><img src="{{var "asset_base"}}/logo.png" alt="{{var "brand"}}" width="28" height="28" style="display:block;width:28px;height:28px;border:0;"></a></td>
<td valign="middle" class="pl-text" style="{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:20px;font-weight:600;{{style "color" "color.text"}}">{{var "brand"}}</td>
</tr></table>
</td></tr>
{{.Body}}
</table>
{{msoIf}}</td></tr></table>{{msoEnd}}
</td></tr></table>
</body>
</html>
- Outlook ignores
max-width, so the surface is capped withmax-widthfor everyone else and wrapped in a fixed-width table only Outlook sees. The coremsoOpenwrites that table from anint. Plain spells it out because its width is a token, and tokens are strings. - Every section is a row (
<tr><td …>), padded with.Section.Topand.Section.Bottom(see Component schemas). - Styles are inline, on every element, because some clients drop or rewrite
<style>. Outlook does not inherit fonts from a table cell, so Plain repeats the font on every block. - Images carry
width, because Outlook sizes images from the attribute, not CSS. Plain's image caps it at the content width:
{{- /* Outlook sizes images from the width attribute, so it is capped at the
content width (size.content). */ -}}
{{- $w := .Attrs.width}}{{if lt $w 1}}{{fail "width must be at least 1"}}{{end}}{{if gt $w 496}}{{$w = 496}}{{end -}}
<tr><td class="pl-px" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};">
{{- if .Attrs.href}}<a href="{{.Attrs.href}}">{{end -}}
<img src="{{.Attrs.src}}" alt="{{.Attrs.alt}}" width="{{$w}}" style="display:block;width:{{px $w}};max-width:100%;height:auto;border:0;">
{{- if .Attrs.href}}</a>{{end}}
{{- if .Attrs.caption}}<p class="pl-muted" style="margin:8px 0 0;{{style "font-family" "font.sans"}}font-size:{{token "size.small"}}px;line-height:{{token "size.small_line"}}px;{{style "color" "color.muted"}}">{{.Attrs.caption}}</p>{{end -}}
</td></tr>
Serve Outlook
html/template strips HTML comments, including the conditional comments Outlook depends on. The core functions are the only way to write them:
| Function | Writes |
|---|---|
msoOpen N | <!--[if mso]><table role="presentation" width="N" align="center" cellpadding="0" cellspacing="0" border="0"><tr><td><![endif]--> |
msoClose | <!--[if mso]></td></tr></table><![endif]--> |
msoIf … msoEnd | <!--[if mso]> … <![endif]-->: markup only Outlook renders |
notMsoOpen … notMsoClose | <!--[if !mso]><!--> … <!--<![endif]-->: markup every client but Outlook renders |
button writes a bulletproof pill button: a VML v:roundrect for Outlook, and an <a> styled as an inline block for everyone else.
- Signature:
button(href template.URL, label, fill, text string, width, height int) template.HTML. - Size: a width or height of 0 means an estimate: 9px per character of the label plus 48px, and 48px tall. The corners are half the height.
- Type: the label is set in
Arial,sans-serifat 15px bold, since Outlook ignores web font stacks. - Colours:
fillandtextmust be a#hexcolour or a colour keyword. Anything else becomesZgotmplZ.
Plain does not use button. Its buttons are a square bgcolor cell with mso-padding-alt, which Outlook renders as padding:
{{define "action"}}
{{- if .Primary}}<table role="presentation" cellpadding="0" cellspacing="0" border="0"><tr><td class="pl-btn" bgcolor="{{token "color.button"}}" style="{{style "background-color" "color.button"}}mso-padding-alt:10px 20px;"><a class="pl-btn-a" href="{{.Href}}" style="display:inline-block;padding:10px 20px;{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:20px;font-weight:600;{{style "color" "color.on_button"}}text-decoration:none;">{{.Label}}</a></td></tr></table>
{{- else}}<a class="pl-link" href="{{.Href}}" style="{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:20px;font-weight:600;{{style "color" "color.accent"}}text-decoration:none;">{{.Label}}{{if .Arrow}} →{{end}}</a>
{{- end}}
{{- end}}
Check every URL
lttr checks every URL that comes from the Markdown or the recipient before a template sees it: url attributes and frontmatter keys, links, action targets, image sources and the unsubscribe URL. A URL from anywhere else, such as a string in a bound Go value or one built in the template, should go through {{url …}}, which applies the same rules:
| URL | Result |
|---|---|
https:// or http:// with a host | Allowed |
mailto: or tel: with an address | Allowed |
| A relative URL | Allowed only with Config.AllowRelativeURLs, for previews |
| Empty, or with whitespace or a control character | An error |
| Any other scheme | An error, such as url "javascript:alert(1)": scheme "javascript" not allowed |
A failed check is a render error, never a silent #. A passing URL keeps its bytes, so tel: links survive html/template.
Support dark mode
dark: true in kit.yaml says the kit has a dark palette. The lint then requires the color-scheme meta, which Plain writes in its head (document writes it for kits that use it). The light palette is inline; the dark one lives in styles.css, keyed to classes on the elements:
@media (prefers-color-scheme:dark){
.pl-canvas{background-color:{{dark "color.canvas"}}!important}
.pl-surface{background-color:{{dark "color.surface"}}!important;border-color:{{dark "color.border"}}!important}
.pl-rule{border-color:{{dark "color.border"}}!important}
.pl-text{color:{{dark "color.text"}}!important}
.pl-muted{color:{{dark "color.muted"}}!important}
.pl-text a,.pl-muted a,.pl-link{color:{{dark "color.accent"}}!important}
.pl-text code{background-color:{{dark "color.subtle"}}!important}
.pl-btn{background-color:{{dark "color.button"}}!important;border-color:{{dark "color.button"}}!important}
.pl-btn .pl-btn-a{color:{{dark "color.on_button"}}!important}
.pl-text s,.pl-muted s{color:{{dark "color.muted"}}!important}
}
[data-ogsc] .pl-text{color:{{dark "color.text"}}!important}
[data-ogsc] .pl-muted{color:{{dark "color.muted"}}!important}
[data-ogsc] .pl-text a,[data-ogsc] .pl-muted a,[data-ogsc] .pl-link{color:{{dark "color.accent"}}!important}
[data-ogsc] .pl-btn .pl-btn-a{color:{{dark "color.on_button"}}!important}
[data-ogsc] .pl-text s{color:{{dark "color.muted"}}!important}
[data-ogsc] .pl-rule{border-color:{{dark "color.border"}}!important}
[data-ogsb] .pl-canvas{background-color:{{dark "color.canvas"}}!important}
[data-ogsb] .pl-surface{background-color:{{dark "color.surface"}}!important}
[data-ogsb] .pl-btn{background-color:{{dark "color.button"}}!important}
[data-ogsb] .pl-text code{background-color:{{dark "color.subtle"}}!important}
- Clients that support
prefers-color-scheme, Apple Mail among them, apply the@mediablock. Eachpl-*class maps to a dark token with!important, which beats the inline light style. - Outlook.com recolours emails itself and marks what it changed with
data-ogsc(text) anddata-ogsb(backgrounds). Repeating the rules under those selectors makes it use the kit's dark palette instead of its own guesses. - Clients that read neither keep the light palette.
{{dark "color.canvas"}}gives a colour token's dark value. The templates write the inline light value on the same element with{{style "color" "color.text"}}(or{{token}}in abgcolorattribute), and a class connects the two.styles.cssis plain text, so a dark value in any CSS colour syntax,hsl()included, is written as is.
Switch this preview to dark to see Plain's dark palette. The preview's scheme is its own toggle, independent of this site's:
Your receipt for order EX-1042
From: Example <hello@example.com>
Next steps
- Plain text: the other half of every message.
- CLI:
lttr kit checkandlttr lint.
Go types and funcs
Register the Go types components bind with kit.Types, add template functions with kit.Funcs, and check such a kit from Go, since the CLI cannot load it.
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.