mime API reference
Build and parse MIME messages (RFC 5322 headers + RFC 2045/2046 bodies): a header-and-boundary structure that is exactly the text orchestration a .j module does well. The transfer codecs (base64, quoted-printable) are delegated to the encoding system library. This is the message-structure foundation the mail clients (SMTP / POP3 / IMAP) build on; it does no networking itself. A Part is a leaf (headers + a decoded body/data with a transfer encoding) or a multipart container (headers + child parts + a boundary); bodies are held decoded as text, encode applies the transfer encoding and parse removes it. RFC 2047 encoded-words are applied automatically on encode and decoded on parse, with encodeWord / decodeWord exposed for manual use. Every leaf carries its raw decoded data (bytes), so binary attachments round-trip; body is the text view, populated for text parts and decoded per the Content-Type charset (UTF-8 by default; ISO-8859 / Windows single-byte codecs are honoured, with a UTF-8 fallback for an unknown label). Attachment filenames are read and written in RFC 2231 extended form (filename*=) when they carry non-ASCII characters. Use walk / attachments / textBodies to pull parts out of a received message.
Import with import "mime.j" as mime;. See the mime guide for prose and examples.
Contents
| Name | What it is |
|---|---|
address | Format an RFC 5322 mailbox: email alone, or Name <email> with the name quoted when it carries a special... |
attachment | Build a base64 leaf part with a filename. |
attachmentBytes | Build a base64 attachment part from raw bytes (images, PDFs, any binary). |
attachments | Return every attachment leaf in the message (see isAttachment). |
body | Return a leaf part's decoded text body. |
contentType | Return a part's media type without parameters (e.g. |
data | Return a leaf part's raw decoded content bytes (any type, including binary attachments). |
decodeWord | Decode every RFC 2047 encoded-word in value, dropping the linear whitespace that separates two adjacent e... |
disposition | Return a part's Content-Disposition (lowercased, without parameters): "attachment", "inline", or "" when th... |
encode | Serialize a part (and its subtree) to a MIME message string with CRLF line endings and the declared transfe... |
encodeWord | Render text as one or more RFC 2047 UTF-8 base64 encoded-words, each kept under the 75-character limit (s... |
filename | Return a part's filename: the Content-Disposition filename parameter, else the Content-Type name parame... |
findParts | Return every leaf whose media type equals mediaType (e.g. |
headerValue | Return a part's header value (case-insensitive) or "". |
isAttachment | Report whether a part is an attachment: Content-Disposition is "attachment", or a filename is set. |
multipart | Build a container part; every child ships under one boundary. |
parse | Read a MIME message string into a Part tree: headers are unfolded, a multipart body is split on its boundar... |
parts | Return a multipart container's child parts. |
text | Build a leaf text part; the body is sent 7bit when ASCII, else quoted-printable. |
textBodies | Return the readable text bodies: text parts (any text/ subtype) that are not attachments (so both the tex... |
walk | Flatten a part tree to its leaf parts (depth-first). |
withHeader | Return a copy of the part with name set (replaced or appended). |
Header | struct: A single header field. |
Part | struct: A MIME part: a leaf (data/body + encoding, parts empty) or a multipart container (parts +... |
Functions
address
mime.address(name as string, email as string)Format an RFC 5322 mailbox: email alone, or Name <email> with the name quoted when it carries a special character.
name- the display name (empty for a bare address)
email- the email address
- Returns
string - the formatted mailbox
attachment
mime.attachment(filename as string, contentType as string, body as string)Build a base64 leaf part with a filename. body is text content (UTF-8); binary bodies are not yet supported.
filename- the attachment filename
contentType- the media type
body- the text content to attach
- Returns
Part - the attachment part
attachmentBytes
mime.attachmentBytes(
filename as string,
contentType as string,
data as bytes)Build a base64 attachment part from raw bytes (images, PDFs, any binary). The binary counterpart to attachment; encode base64-wraps the data and parse restores it, so it round-trips intact.
filename- the attachment filename
contentType- the media type (e.g. "image/png")
data- the raw file content
- Returns
Part - the attachment part
attachments
mime.attachments(part as Part)Return every attachment leaf in the message (see isAttachment). Each part's bytes are data($part) and its name filename($part).
part- the parsed message (or any subtree)
- Returns
list of Part - the attachment parts
body
mime.body(part as Part)Return a leaf part's decoded text body.
part- the leaf part
- Returns
string - the decoded body text
contentType
mime.contentType(part as Part)Return a part's media type without parameters (e.g. "text/plain").
part- the part to read
- Returns
string - the media type
data
mime.data(part as Part)Return a leaf part's raw decoded content bytes (any type, including binary attachments). This is the byte-accurate counterpart to body.
part- the leaf part
- Returns
bytes - the raw decoded content
decodeWord
mime.decodeWord(value as string)Decode every RFC 2047 encoded-word in value, dropping the linear whitespace that separates two adjacent encoded-words (as a reader should). A word that fails to decode is left verbatim, as RFC 2047 6.3 says to.
value- the header value possibly carrying encoded-words
- Returns
string - the decoded text
disposition
mime.disposition(part as Part)Return a part's Content-Disposition (lowercased, without parameters): "attachment", "inline", or "" when the header is absent.
part- the part to read
- Returns
string - the disposition
encode
mime.encode(part as Part)Serialize a part (and its subtree) to a MIME message string with CRLF line endings and the declared transfer encodings applied.
part- the part to serialize
- Returns
string - the encoded MIME message
encodeWord
mime.encodeWord(text as string)Render text as one or more RFC 2047 UTF-8 base64 encoded-words, each kept under the 75-character limit (split on rune boundaries, never mid-character) and folded with CRLF + space when more than one is needed.
text- the text to encode
- Returns
string - the encoded-word sequence
filename
mime.filename(part as Part)Return a part's filename: the Content-Disposition filename parameter, else the Content-Type name parameter. RFC 2231 extended / continued forms (filename*=, filename*0=...) and RFC 2047 encoded-words are both decoded; "" when neither is present.
part- the part to read
- Returns
string - the filename
findParts
mime.findParts(part as Part, mediaType as string)Return every leaf whose media type equals mediaType (e.g. "text/html").
part- the parsed message (or any subtree)
mediaType- the exact media type to match (case-insensitive)
- Returns
list of Part - the matching leaf parts
headerValue
mime.headerValue(part as Part, name as string)Return a part's header value (case-insensitive) or "".
part- the part to read
name- the header name
- Returns
string - the header value, or "" when absent
isAttachment
mime.isAttachment(part as Part)Report whether a part is an attachment: Content-Disposition is "attachment", or a filename is set.
part- the part to read
- Returns
bool - true when the part is an attachment
multipart
mime.multipart(subtype as string, boundary as string, parts as list of Part)Build a container part; every child ships under one boundary.
subtype- the multipart subtype (e.g. "mixed", "alternative")
boundary- the boundary delimiter separating children
parts- the child parts
- Returns
Part - the multipart container part
parse
mime.parse(text as string)Read a MIME message string into a Part tree: headers are unfolded, a multipart body is split on its boundary (recursively), and a leaf body is transfer-decoded. A message whose structure cannot be read correctly throws.
text- the MIME message text
- Returns
Part - the parsed part tree
- Throws
Error - kind "mime" for a header line without a colon (or with a malformed field name), a multipart type without a boundary, a multipart body without its closing delimiter or without a part, a malformed base64 / quoted-printable body, or a text body that is not valid in its declared charset; kind "limit" for multipart nesting deeper than 100
parts
mime.parts(part as Part)Return a multipart container's child parts.
part- the multipart container
- Returns
list of Part - the child parts
text
mime.text(contentType as string, body as string)Build a leaf text part; the body is sent 7bit when ASCII, else quoted-printable. charset=utf-8 is appended to the content type.
contentType- the media type (e.g. "text/plain")
body- the decoded text body
- Returns
Part - the leaf text part
textBodies
mime.textBodies(part as Part)Return the readable text bodies: text parts (any text/ subtype) that are not attachments (so both the text/plain and text/html alternatives of a message).
part- the parsed message (or any subtree)
- Returns
list of Part - the text body parts
walk
mime.walk(part as Part)Flatten a part tree to its leaf parts (depth-first). A leaf is a part with no child parts; the building block for attachments / textBodies.
part- the part (leaf or container) to walk
- Returns
list of Part - every leaf part, in document order
withHeader
mime.withHeader(part as Part, name as string, value as string)Return a copy of the part with name set (replaced or appended).
part- the part to copy
name- the header name to set
value- the header value
- Returns
Part - the updated copy
Structs
Header
A single header field.
def struct Header {
name as string,
value as string
};name- the field name (e.g. "Subject")
value- the field value
Part
A MIME part: a leaf (data/body + encoding, parts empty) or a multipart container (parts + boundary, body/data empty).
def struct Part {
headers as list of Header,
body as string,
encoding as string,
parts as list of Part,
boundary as string,
data as bytes
};headers- the part's header fields
body- the decoded text body of a text part ("" for a binary part)
encoding- the transfer encoding ("7bit", "base64", "quoted-printable")
parts- the child parts of a multipart container
boundary- the multipart boundary delimiter
data- the raw decoded content of a leaf (any type, incl. binary)