Message and MIME
Template.Render (see Profiles and config) returns a *lttr.Message: the envelope, both bodies and any extra headers. A Sender — SMTP or your own — delivers it; Message.MIME turns it into the bytes a mail server accepts.
Read the rendered fields
type Message struct {
Template, Profile string
From, To mail.Address
Subject, Preheader string
HTML, Text string
Warnings []string
}
Template and Profile name what produced the message — the compiled template's name and the profile its frontmatter chose — and are never written to the wire; Profile is what your own code routes on (see Route on Message.Profile). Warnings carries non-fatal findings from render, for example HTML over 90 KB but under the 100 KB hard limit.
Message.Clone() returns a deep copy: changing the copy's headers or warnings never touches the original. SMTP.Send clones before adding its own headers, so the same *Message can be sent through more than one Sender without them stepping on each other.
Set a custom header
func (m *Message) SetHeader(name, value string) error
func (m *Message) Header(name string) string
func (m *Message) Headers() []string
SetHeader replaces any earlier value for name. Three things make it fail:
- The name itself is invalid.
namemust be a non-empty run of printable ASCII without:; anything else fails withheader name %q is invalidbefore the reserved-name or value checks even run. - The name is reserved.
From,To,Cc,Bcc,Subject,Date,Message-Id,Mime-Version,Content-TypeandContent-Transfer-Encodingare reserved:MIMEwrites the first group itself, andMessagehas noCcorBccfield — there is only ever one recipient,Message.To— so a header by either name would functionally add a recipient no other check accounts for.List-UnsubscribeandList-Unsubscribe-Postare reserved for a third reason: they can only be set throughSetListUnsubscribe, which enforceshttps. - The value is unsafe. A value containing CR, LF or NUL is refused — this is what stops header injection. A value must also be printable ASCII (space and tab allowed); raw 8-bit or control bytes would make an invalid RFC 5322 header, so they are refused rather than silently written. Encode such a value (for example with
mime.QEncoding) before callingSetHeader.
Names are matched case-insensitively and stored under their canonical form (textproto.CanonicalMIMEHeaderKey), so m.Header("x-order-id") and m.Header("X-Order-Id") return the same value. Headers() returns the canonical names, sorted — the order MIME writes them in, between MIME-Version and Content-Type.
A profile's Headers go through the same SetHeader, applied to every message the profile produces; Render calls it for you, after the compiled template's own checks.
Set the one-click unsubscribe header
func (m *Message) SetListUnsubscribe(u string) error
SetListUnsubscribe sets the RFC 8058 one-click headers:
List-Unsubscribe: <https://example.com/u/abc123>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
u must be an absolute https URL with a host; anything else is an error, including a URL containing CR, LF, NUL, a space, <, > or a byte outside printable ASCII. Render calls this itself whenever the message's profile has ListUnsubscribe: true, passing the recipient's UnsubscribeURL — which is why such a profile needs an https UnsubscribeURL on every recipient it renders for; see ListUnsubscribe.
Serialise to MIME
func (m *Message) MIME(date time.Time) ([]byte, error)
MIME writes an RFC 5322 message with CRLF line endings: multipart/alternative, text/plain first, then text/html, both UTF-8 and quoted-printable. The headers are written in a fixed order — From, To, Subject, Date, Message-ID, MIME-Version, then the custom headers sorted by name, then Content-Type. Here is a real render of one of the Quiet kit's marketing samples (lttr render -t campaign.harvest_week), whose profile sets ListUnsubscribe:
From: "Loam" <hello@loam.example>
To: "Maya" <maya@example.com>
Subject: Harvest Week: 48 hours of the valley's best
Date: Thu, 24 Sep 2026 12:00:00 +0000
Message-ID: <aef49d391dc60802dfc7f1f4a7826067@loam.example>
MIME-Version: 1.0
List-Unsubscribe: <https://loam.example/unsubscribe>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
Content-Type: multipart/alternative;
boundary="79e5fab8903aea95295e9016a2648264"
--79e5fab8903aea95295e9016a2648264
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf-8
LOAM
HARVEST WEEK =C2=B7 26=E2=80=9327 SEPT
Grown in the valley. Delivered across it.
... (the text part continues, then the text/html part, both quoted-printable)
produced by a hand-built message going through the same call:
package main
import (
"net/mail"
"os"
"time"
"github.com/sulv-io/lttr"
)
func main() {
m := <tr.Message{
Profile: "orders",
From: mail.Address{Name: "Acme", Address: "orders@acme.example"},
To: mail.Address{Name: "Kim", Address: "kim@example.com"},
Subject: "Your order is on its way",
HTML: "<p>On its way.</p>",
Text: "On its way.",
}
must(m.SetHeader("X-Order-Id", "A-1042"))
must(m.SetListUnsubscribe("https://acme.example/u/abc123"))
eml, err := m.MIME(time.Now())
must(err)
os.Stdout.Write(eml)
}
func must(err error) {
if err != nil {
panic(err)
}
}
A few details worth relying on:
- The Subject and both display names are RFC 2047 encoded when they are not plain ASCII;
mime.QEncoding.Encodeleaves already-ASCII values unchanged. Message-IDis<{32 hex characters}@{From's domain}>, and the MIME boundary is a second 32-character hex string — both fromcrypto/rand, so two calls toMIMEnever collide.- A line is folded at a space so it stays within 78 bytes where the value allows it (RFC 5322 §2.1.1); a word longer than that stays whole on its line. A line that cannot be folded to 998 bytes or fewer is an error.
- Both bodies are required.
MIMEitself refuses an emptyTextorHTML—"text part is empty"or"html part is empty"— rather than serialising a one-sided message. This isn't somethingRenderchecks up front; it'sMIME's own safety net for a hand-builtMessagetoo. MIMEre-checksFrom,ToandSubjecteven thoughRenderalready checked them, so a hand-builtMessagegets the same guarantees a compiled template's output does.
Know what Render checks before MIME sees it
Template.Render checks the following itself, before a message ever reaches MIME:
- The subject must be sane. Empty (after trimming whitespace), or containing CR, LF or NUL, fails at the
subjectfrontmatter key rather than insideMIME. - The recipient address must be
local@domain. The samecheckAddressruleMIMEuses — an RFC 5322 dot-atom local part and a dot-separated hostname, no quoted local parts, no address literals, no internationalised addresses (SMTPUTF8) in this version.net/smtpwritesMAIL FROM:<%s>andRCPT TO:<%s>raw, so an address with a stray>or a second@could let the SMTP envelope disagree with the address your application already saw; rejecting it at render closes that gap. - Display names must be injection-safe. A recipient name containing CR, LF or NUL fails render with
render: recipient name contains CR, LF or NUL.
Stay under the size limit
Gmail clips a message whose HTML is over 102 KB and hides the rest behind a link. Render enforces a margin below that:
| Rendered HTML | Result |
|---|---|
| 90 KB (92,160 bytes) or less | Nothing |
| Over 90 KB | A warning in Message.Warnings: name: warning: html is N bytes; gmail clips at 102 KB |
| 100 KB (102,400 bytes) or more | An error: render: html is N bytes; gmail clips at 102 KB |
Every Quiet example renders well under the warning threshold. A long bound list (products, a big receipt, stock) is the usual way to grow past it.
Next steps
- Profiles and config for
Profile.Headers,ListUnsubscribeand routing onMessage.Profile. - SMTP for the one
Senderthe library ships.