Build RFC 5322 / MIME messages as Bytes, safe for LLM-supplied input.

  • No header injection. CR, LF and other control characters in the subject and extra header values are folded to a single space; addresses containing them, malformed addresses and malformed header names are rejected with an Err. Structural headers (Content-Type, MIME-Version, ...) cannot be overridden through headers.
  • Non-ASCII subjects, header values and display names are RFC 2047 encoded (=?UTF-8?B?...?=, folded at 75 characters); non-ASCII attachment names use RFC 2231 filename*.
  • Bodies. Plain text, HTML, or both (multipart/alternative), plus attachments (multipart/mixed). ASCII text with short lines is sent 7bit; anything else is base64 with 76-character lines. Attachments are base64 (a forwarded message/rfc822 is 8bit). Everything is concatenated as Bytes, so binary attachments arrive intact.

Message fields (all optional except a body for sending): from, to, cc, bcc, reply-to (a Str such as "Ada <ada@example.com>, bob@example.com", a Vec of such Strs, or {name, email} Maps), subject, text, html, attachments (a Vec of {filename, content, mime-type?, content-id?, inline?}, content being Bytes or a Str), in-reply-to, references (a Message-ID Str or Vec), and headers (extra headers as a Map). Gmail adds Date and Message-ID and strips Bcc from delivered copies.

::mime ::google::gmail::mime

raw ::mime/build-message({
  to: ["Zoë Example <zoe@example.com>"],
  subject: "Résumé attached",
  text: "Hi Zoë, see attached.",
  html: "<p>Hi Zoë, see attached.</p>",
  attachments: [{filename: "résumé.pdf", content: Bytes([37, 80, 68, 70]), mime-type: "application/pdf"}]
})

Values

ATOM_PHRASE_PATTERN

ATOM_PHRASE_PATTERN: Str "^[A-Za-z0-9!#$%&'*+/=?^_`{|}~ -]+$"

CONTROL_PATTERN

CONTROL_PATTERN: Str "[\\x00-\\x08\\x0A-\\x1F\\x7F]+"

EMAIL_PATTERN

EMAIL_PATTERN: Str "^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$"

HEADER_NAME_PATTERN

HEADER_NAME_PATTERN: Str "^[!-9;-~]+$"

LINE_BREAK_PATTERN

LINE_BREAK_PATTERN: Str "[\\r\\n]"

MAX_PLAIN_HEADER

MAX_PLAIN_HEADER: Int 900

MIME_TYPE_PATTERN

MIME_TYPE_PATTERN: Str "^[A-Za-z0-9!#$&^_.+-]+/[A-Za-z0-9!#$&^_.+-]+$"

RESERVED_HEADERS

RESERVED_HEADERS: Vec [
  "from",
  "to",
  "cc",
  "bcc",
  "reply-to",
  "subject",
  "in-reply-to",
  "references",
  "mime-version",
  "content-type",
  "content-transfer-encoding",
  "content-disposition"
]

WORD_BYTES

WORD_BYTES: Int 39

Functions

attachment-error

fn (attachment: Any): Str?

Why an attachment Map is unusable (missing filename or content, a bad MIME type), or null (pure).

Example

::google::gmail::mime/attachment-error({filename: "a.txt", content: "hi"})  // null
::google::gmail::mime/attachment-error({filename: "a.txt"})  // "google-gmail: attachment a.txt has no content"

attachment-mime-type

fn (attachment: Map): Str

The MIME type for an attachment: its mime-type, else guessed from the filename, else application/octet-stream.

Example

::google::gmail::mime/attachment-mime-type({filename: "a.pdf"})  // "application/pdf"
::google::gmail::mime/attachment-mime-type({filename: "a.pdf", mime-type: "text/plain"})  // "text/plain"

attachment-part

fn (attachment: Map): Bytes

An attachment part as Bytes: Content-Type with name, Content-Disposition (attachment, or inline with a content-id), and the content base64 encoded in 76-character lines. A message/rfc822 attachment (a forwarded email) is embedded as is, 8bit.

Example

Str(::google::gmail::mime/attachment-part({filename: "a.txt", content: "hi", mime-type: "text/plain"}))
// "Content-Type: text/plain; name=\"a.txt\"\r\nContent-Disposition: attachment; filename=\"a.txt\"\r\nContent-Transfer-Encoding: base64\r\n\r\naGk="

build-body

fn (message: Map, boundary: Str): Bytes

The body entity (its Content-Type headers and content) as Bytes: text, html, or both as multipart/alternative; wrapped in multipart/mixed with the attachments when there are any. Nested boundaries derive from boundary (a_ / m_ prefixes).

Example

Str(::google::gmail::mime/build-body({text: "Hi"}, "b1"))
// "Content-Type: text/plain; charset=\"UTF-8\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\nHi"

contains(Str(::google::gmail::mime/build-body({text: "Hi", html: "<p>Hi</p>"}, "b1")), "multipart/alternative")  // true

build-headers

fn (message: Map): Str

The top-level header block of a message (pure), each line ending in CRLF, ending with MIME-Version: 1.0: From, To, Cc, Bcc, Reply-To, Subject, In-Reply-To, References, then message.headers. Returns an Err for an invalid address or header name.

Example

::google::gmail::mime/build-headers({to: "ada@example.com", subject: "Hi"})  // "To: ada@example.com\r\nSubject: Hi\r\nMIME-Version: 1.0\r\n"

build-message

fn (message: Map): Bytes
fn (message: Map, boundary: Str): Bytes

Build a complete RFC 5322 message as Bytes from a message Map (see the namespace doc for the fields). Returns an Err for invalid addresses, header names or attachments. Pass boundary for reproducible output in tests.

raw ::google::gmail::mime/build-message({to: "ada@example.com", subject: "Hi", text: "Hello!"})
fn (metadata: Map, message: Bytes, boundary: Str): Bytes

A Gmail media-upload body as Bytes (multipart/related): a JSON metadata part (nulls dropped) and the raw message as message/rfc822, concatenated as bytes. Send it with Content-Type: multipart/related; boundary=<boundary>.

Example

raw ::google::gmail::mime/build-message({to: "ada@example.com", subject: "Hi", text: "Hello!"}, "b1")
body ::google::gmail::mime/build-related({threadId: "t1"}, raw, "rel1")
starts-with(Str(body), "--rel1\r\nContent-Type: application/json")  // true

clean-header-value

fn (value: Any): Str

Fold every run of CR, LF and other control characters (tab excepted) to one space and trim, so a value can never start a new header line.

Example

::google::gmail::mime/clean-header-value("Hi\r\nBcc: x@example.com")  // "Hi Bcc: x@example.com"

emails

fn (value: Any): Vec

Just the email addresses of an address field, lowercased; invalid entries are skipped (pure). Handy for comparing senders.

Example

::google::gmail::mime/emails("Ada <ADA@example.com>, not-an-address")  // ["ada@example.com"]

encode-header-text

fn (value: Any): Str

A header value safe to emit: control characters folded (clean-header-value), then RFC 2047 encoded when it is not ASCII or is very long.

Example

::google::gmail::mime/encode-header-text("Weekly report")  // "Weekly report"
::google::gmail::mime/encode-header-text("Résumé")  // "=?UTF-8?B?UsOpc3Vtw6k=?="

encode-phrase

fn (name: Str): Str

A display name for an address header: as is when it is plain ASCII words, a quoted string when it has specials ("Doe, Jane"), or RFC 2047 encoded when it is not ASCII.

Example

::google::gmail::mime/encode-phrase("Ada Lovelace")  // "Ada Lovelace"
::google::gmail::mime/encode-phrase("Doe, Jane")  // "\"Doe, Jane\""

encode-words

fn (text: Str): Str

RFC 2047 encode text as UTF-8 B encoded words, split on character boundaries by byte count so each word stays within 75 characters and a character is never cut in two, joined with a folding CRLF SP: encode-words("Zoë") is "=?UTF-8?B?Wm/Dqw==?=".

Example

::google::gmail::mime/encode-words("Zoë")  // "=?UTF-8?B?Wm/Dqw==?="

filename-params

fn (filename: Str): Map

The name (Content-Type) and filename (Content-Disposition) parameters for a file name: quoted ASCII, or RFC 2047 / RFC 2231 encoded when not ASCII.

Example

::google::gmail::mime/filename-params("report.pdf")  // {name: "; name=\"report.pdf\"", filename: "; filename=\"report.pdf\""}

format-address

fn (address: Map): Str

Render {name, email} for a header: Ada <ada@example.com>, with the name quoted or RFC 2047 encoded as needed.

Example

::google::gmail::mime/format-address({name: "Ada", email: "ada@example.com"})  // "Ada <ada@example.com>"
::google::gmail::mime/format-address({name: null, email: "bob@example.com"})  // "bob@example.com"

format-addresses

fn (addresses: Vec): Str

Render a Vec of {name, email} as one header value, comma separated (folded when long).

Example

::google::gmail::mime/format-addresses([{name: "Ada", email: "ada@example.com"}, {name: null, email: "bob@example.com"}])  // "Ada <ada@example.com>, bob@example.com"

has-line-break

fn (s: Str): Bool

True when s contains a CR or LF.

Example

::google::gmail::mime/has-line-break("one\ntwo")  // true
::google::gmail::mime/has-line-break("one two")  // false

is-ascii

fn (s: Str): Bool

True when s has only 7-bit ASCII characters.

Example

::google::gmail::mime/is-ascii("hello")  // true
::google::gmail::mime/is-ascii("Zoë")  // false

is-email

fn (email: Any): Bool

True when email is a plain local@domain.tld address (ASCII; no display name, spaces or line breaks).

Example

::google::gmail::mime/is-email("ada@example.com")  // true
::google::gmail::mime/is-email("Ada <ada@example.com>")  // false

multipart

fn (subtype: Str, parts: Vec, boundary: Str): Bytes

A multipart/<subtype> entity as Bytes: its Content-Type header, then each part (Bytes, already carrying its own headers) between boundary delimiters.

Example

part ::google::gmail::mime/text-part("Hi", "plain")
raw ::google::gmail::mime/multipart("mixed", [part], "b1")
starts-with(Str(raw), "Content-Type: multipart/mixed; boundary=\"b1\"")  // true

new-boundary

fn (): Str

A random MIME boundary. It contains =_, which never occurs in base64 content.

Example

starts-with(::google::gmail::mime/new-boundary(), "=_hot_")  // true

normalize-newlines

fn (text: Str): Str

Convert LF, CR and CRLF line endings to CRLF (what SMTP requires).

Example

::google::gmail::mime/normalize-newlines("a\nb\r\nc")  // "a\r\nb\r\nc"

parse-addresses

fn (value: Any): Vec

Parse an address field (Str list, Vec, or {name, email} Map) into a Vec of {name, email}, or an Err naming the first invalid address.

Example

::google::gmail::mime/parse-addresses("Ada <ada@example.com>, bob@example.com")  // [{name: "Ada", email: "ada@example.com"}, {name: null, email: "bob@example.com"}]
is-err(::google::gmail::mime/parse-addresses("not-an-address"))  // true

read-address

fn (value: Any): Map

Parse one address ("ada@example.com", "Ada Lovelace <ada@example.com>", or {name, email}) into {name, email, error} (pure); error is null when the address is valid.

Example

::google::gmail::mime/read-address("Ada <ada@example.com>")  // {name: "Ada", email: "ada@example.com", error: null}
::google::gmail::mime/read-address("nope").error  // "google-gmail: invalid email address \"nope\""

split-address-list

fn (value: Str): Vec

Split an address-list Str on commas outside double quotes: "\"Doe, Jane\" <j@x.io>, bob@x.io" gives two entries.

Example

::google::gmail::mime/split-address-list("\"Doe, Jane\" <j@example.com>, bob@example.com")  // ["\"Doe, Jane\" <j@example.com>", "bob@example.com"]

text-part

fn (text: Str, subtype: Str): Bytes

A text/<subtype>; charset="UTF-8" part (headers, blank line, body) as Bytes: 7bit when the text is ASCII with lines under 999 characters, else base64. Line endings become CRLF.

Example

Str(::google::gmail::mime/text-part("Hi", "plain"))  // "Content-Type: text/plain; charset=\"UTF-8\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\nHi"

to-bytes

fn (content: Any): Bytes

Content as Bytes: Bytes pass through, a Str becomes its UTF-8 bytes, null is empty, anything else is JSON-encoded.

Example

Str(::google::gmail::mime/to-bytes("Hi"))  // "Hi"
length(::google::gmail::mime/to-bytes(null))  // 0
Str(::google::gmail::mime/to-bytes({a: 1}))  // "{\"a\":1}"

to-raw

fn (message: Any): Str

Base64url-encode a message (Bytes or Str) for the Gmail raw field.

Example

::google::gmail::mime/to-raw("Hi")  // "SGk"

wrap-base64

fn (encoded: Str): Str

Break a base64 Str into CRLF-separated lines of at most 76 characters (RFC 2045).

Example

encoded join(map(range(80), (i) { "A" }), "")
map(split(::google::gmail::mime/wrap-base64(encoded), "\r\n"), length)  // [76, 4]