Skip to content
Jennifer Programming Language

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

NameWhat it is
addressFormat an RFC 5322 mailbox: email alone, or Name <email> with the name quoted when it carries a special...
attachmentBuild a base64 leaf part with a filename.
attachmentBytesBuild a base64 attachment part from raw bytes (images, PDFs, any binary).
attachmentsReturn every attachment leaf in the message (see isAttachment).
bodyReturn a leaf part's decoded text body.
contentTypeReturn a part's media type without parameters (e.g.
dataReturn a leaf part's raw decoded content bytes (any type, including binary attachments).
decodeWordDecode every RFC 2047 encoded-word in value, dropping the linear whitespace that separates two adjacent e...
dispositionReturn a part's Content-Disposition (lowercased, without parameters): "attachment", "inline", or "" when th...
encodeSerialize a part (and its subtree) to a MIME message string with CRLF line endings and the declared transfe...
encodeWordRender text as one or more RFC 2047 UTF-8 base64 encoded-words, each kept under the 75-character limit (s...
filenameReturn a part's filename: the Content-Disposition filename parameter, else the Content-Type name parame...
findPartsReturn every leaf whose media type equals mediaType (e.g.
headerValueReturn a part's header value (case-insensitive) or "".
isAttachmentReport whether a part is an attachment: Content-Disposition is "attachment", or a filename is set.
multipartBuild a container part; every child ships under one boundary.
parseRead a MIME message string into a Part tree: headers are unfolded, a multipart body is split on its boundar...
partsReturn a multipart container's child parts.
textBuild a leaf text part; the body is sent 7bit when ASCII, else quoted-printable.
textBodiesReturn the readable text bodies: text parts (any text/ subtype) that are not attachments (so both the tex...
walkFlatten a part tree to its leaf parts (depth-first).
withHeaderReturn a copy of the part with name set (replaced or appended).
Headerstruct: A single header field.
Partstruct: A MIME part: a leaf (data/body + encoding, parts empty) or a multipart container (parts +...

Functions

address

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
mime.body(part as Part)

Return a leaf part's decoded text body.

part
the leaf part
Returns string
the decoded body text

contentType

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
mime.parts(part as Part)

Return a multipart container's child parts.

part
the multipart container
Returns list of Part
the child parts

text

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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).

jennifer
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)