Karta.Mail.Message (Karta v1.0.0)

MIME messages (RFC 2045-2049): a parsed view for reading, and building messages Karta writes.

parse/2 gives a tree of Karta.Mail.Parts. Every part keeps its header fields' raw bytes and its body exactly as received; decoding (transfer encoding, charset) happens only when asked for (decoded_body/1, text/1). DKIM and ARC must work on the raw message and on Karta.Mail.Headers, never on anything this module re-encodes.

Parsing is bounded: header limits as in Karta.Mail.Headers.split/2, multipart nesting at most :max_depth deep (default 20) and at most :max_parts entities in all (default 500). A multipart body without its closing delimiter ends at the end of the data, as most readers do.

map_leaves/3 rewrites chosen leaf parts and keeps every other byte; build/2 writes a message from parts given as data; see their docs.

Summary

Functions

Builds a message. spec holds

True for a content id build/2 writes: 1 to 250 printable ASCII characters other than white space, <, >, ", \ and ;.

The body with its Content-Transfer-Encoding removed.

The part's file name: Content-Disposition filename, else Content-Type name.

A date for the Date and Received header fields (RFC 5322 §3.3), in UTC.

All leaf parts (everything that is not multipart), depth first.

Rewrites leaf parts of a message and keeps every other byte: the header blocks, the preambles, delimiters and epilogues of multiparts, and every part fun keeps.

Parses a whole message. See the module doc for the limits.

The readable content: the first inline text/plain and text/html parts without a file name (as UTF-8, read leniently), and every other leaf part as an attachment with its decoded data.

The decoded body as UTF-8 text, using the part's charset (see Karta.Mail.Charset).

Functions

build(spec, opts \\ [])

@spec build(map(), keyword()) :: binary()

Builds a message. spec holds:

  • :from (required) - one Karta.Mail.Address or a list of them; :sender, :to, :cc, :reply_to likewise (optional).
  • :subject - text; non-ASCII becomes encoded words.
  • :date (required) - a DateTime.
  • :message_id (required) - id-left@id-right, written in angle brackets.
  • :headers - extra {name, value} fields, e.g. {"List-Unsubscribe", "<...>"}; non-ASCII values become encoded words.
  • :text, :html - the body as UTF-8 text (at least one of them, or an attachment).
  • :attachments - maps with :content_type, :data and optionally :filename. A message/rfc822 attachment is written as it is (RFC 2046 §5.2.1), so it must have CRLF lines of at most 998 octets; every other attachment is base64.
  • :inline - parts the HTML refers to as cid:<content_id> (images): maps with :content_type, :data, :content_id and optionally :filename; base64, Content-Disposition: inline.

Text parts are UTF-8 in quoted-printable, attachments base64. Text and HTML together become multipart/alternative; inline parts wrap the text in multipart/related (RFC 2387); attachments wrap that in multipart/mixed. Lines end in CRLF, header lines are folded (at most 78 octets where the words allow). utf8: true writes non-ASCII names and addresses as UTF-8 (for SMTPUTF8, RFC 6532) instead of encoded words. Raises ArgumentError for missing or unsafe input.

content_id?(id)

@spec content_id?(term()) :: boolean()

True for a content id build/2 writes: 1 to 250 printable ASCII characters other than white space, <, >, ", \ and ;.

decoded_body(part)

@spec decoded_body(Karta.Mail.Part.t()) :: binary()

The body with its Content-Transfer-Encoding removed.

filename(part)

@spec filename(Karta.Mail.Part.t()) :: String.t() | nil

The part's file name: Content-Disposition filename, else Content-Type name.

format_date(date)

@spec format_date(DateTime.t()) :: String.t()

A date for the Date and Received header fields (RFC 5322 §3.3), in UTC.

leaves(part)

@spec leaves(Karta.Mail.Part.t()) :: [Karta.Mail.Part.t()]

All leaf parts (everything that is not multipart), depth first.

map_leaves(raw, fun, opts \\ [])

@spec map_leaves(
  binary(),
  (Karta.Mail.Part.t() ->
     :keep | {:body, binary()} | {:body, binary(), String.t()}),
  keyword()
) :: {:ok, binary()} | {:error, atom()}

Rewrites leaf parts of a message and keeps every other byte: the header blocks, the preambles, delimiters and epilogues of multiparts, and every part fun keeps.

fun gets each leaf part (those of leaves/1, depth first) and returns :keep, {:body, body} (the new body, already transfer-encoded) or {:body, body, encoding}, which also sets the part's Content-Transfer-Encoding field to encoding. The message is parsed as by parse/2, with the same options, limits and errors.

parse(raw, opts \\ [])

@spec parse(binary(), keyword()) :: {:ok, Karta.Mail.Part.t()} | {:error, atom()}

Parses a whole message. See the module doc for the limits.

summary(message, opts \\ [])

@spec summary(Karta.Mail.Part.t(), keyword()) :: %{
  text: String.t() | nil,
  html: String.t() | nil,
  attachments: [
    %{filename: String.t() | nil, content_type: String.t(), data: binary()}
  ]
}

The readable content: the first inline text/plain and text/html parts without a file name (as UTF-8, read leniently), and every other leaf part as an attachment with its decoded data.

attachments: false leaves the other leaf parts alone, so that a format that never sends them does not decode them: attachments is then [] (the count is not available).

text(part)

@spec text(Karta.Mail.Part.t()) ::
  {:ok, String.t()} | {:error, :unsupported_charset | :invalid}

The decoded body as UTF-8 text, using the part's charset (see Karta.Mail.Charset).