Sending

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.

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

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

message.go
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. name must be a non-empty run of printable ASCII without :; anything else fails with header name %q is invalid before the reserved-name or value checks even run.
  • The name is reserved. From, To, Cc, Bcc, Subject, Date, Message-Id, Mime-Version, Content-Type and Content-Transfer-Encoding are reserved: MIME writes the first group itself, and Message has no Cc or Bcc field — 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-Unsubscribe and List-Unsubscribe-Post are reserved for a third reason: they can only be set through SetListUnsubscribe, which enforces https.
  • 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 calling SetHeader.

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

message.go
func (m *Message) SetListUnsubscribe(u string) error

SetListUnsubscribe sets the RFC 8058 one-click headers:

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

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

harvest_week.eml
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:

main.go
package main

import (
    "net/mail"
    "os"
    "time"

    "github.com/sulv-io/lttr"
)

func main() {
    m := &lttr.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.Encode leaves already-ASCII values unchanged.
  • Message-ID is <{32 hex characters}@{From's domain}>, and the MIME boundary is a second 32-character hex string — both from crypto/rand, so two calls to MIME never 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. MIME itself refuses an empty Text or HTML — "text part is empty" or "html part is empty" — rather than serialising a one-sided message. This isn't something Render checks up front; it's MIME's own safety net for a hand-built Message too.
  • MIME re-checks From, To and Subject even though Render already checked them, so a hand-built Message gets 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 subject frontmatter key rather than inside MIME.
  • The recipient address must be local@domain. The same checkAddress rule MIME uses — 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/smtp writes MAIL FROM:<%s> and RCPT 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 HTMLResult
90 KB (92,160 bytes) or lessNothing
Over 90 KBA warning in Message.Warnings: name: warning: html is N bytes; gmail clips at 102 KB
100 KB (102,400 bytes) or moreAn 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, ListUnsubscribe and routing on Message.Profile.
  • SMTP for the one Sender the library ships.
Copyright © 2026