Karta.Mail.Address (Karta v1.0.0)

Addresses in header fields (RFC 5322 §3.4, with UTF-8 from RFC 6532): From, Sender, Reply-To, To, Cc and Bcc.

This parser decides which domain a message claims to come from, so it follows the grammar exactly and refuses anything it cannot read with certainty, instead of guessing:

  • Comments are parsed (nested, with quoted pairs) and dropped, so victim@bank.example (<me@ours.example>) is the address victim@bank.example. Taking the text after the last < instead is the classic mistake that lets a comment pick the authorised domain.
  • Quoted strings, quoted pairs, domain literals, groups and the obsolete forms of RFC 5322 §4.4 (CFWS around dots and @, empty list elements, routes in angle addresses) are understood.
  • Unterminated quotes, comments or angle addresses, stray specials, text after an address, control characters (other than tab), CR or LF that do not fold, and invalid UTF-8 make the whole value invalid.
  • Comments nest at most 32 deep.

Values may be passed folded or unfolded.

Summary

Types

t()

A mailbox: the display name (quoting and escapes removed; encoded words are left as they are), the local part (likewise) and the domain as written.

Functions

The address as local@domain. The local part is quoted when it is not a dot-atom, e.g. "john smith"@example.com.

The domain in lower-case ASCII form (A-labels), for comparing domains; :error for a domain literal or anything Karta.Mail.Punycode.to_ascii/1 refuses.

Renders a mailbox for a header field Karta writes: Name <local@domain>, or the bare address when there is no name.

Parses an address-list (To, Cc, Bcc, Reply-To): mailboxes and groups.

Parses exactly one mailbox (Sender).

Parses a mailbox-list (From): mailboxes only; a group is an error.

Types

group()

@type group() :: {:group, String.t(), [t()]}

t()

@type t() :: %Karta.Mail.Address{
  domain: String.t(),
  local: String.t(),
  name: String.t() | nil
}

A mailbox: the display name (quoting and escapes removed; encoded words are left as they are), the local part (likewise) and the domain as written.

Functions

address(address)

@spec address(t()) :: String.t()

The address as local@domain. The local part is quoted when it is not a dot-atom, e.g. "john smith"@example.com.

ascii_domain(address)

@spec ascii_domain(t()) :: {:ok, String.t()} | :error

The domain in lower-case ASCII form (A-labels), for comparing domains; :error for a domain literal or anything Karta.Mail.Punycode.to_ascii/1 refuses.

format(mailbox, opts \\ [])

@spec format(t(), keyword()) :: String.t()

Renders a mailbox for a header field Karta writes: Name <local@domain>, or the bare address when there is no name.

A name made of plain words is written as is, other ASCII names as a quoted string, and non-ASCII names as encoded words (RFC 2047) - or as UTF-8 with utf8: true, for messages sent with SMTPUTF8 (RFC 6532). A non-ASCII address needs utf8: true. Raises ArgumentError for control characters or a non-ASCII address without utf8: true.

parse_address_list(value)

@spec parse_address_list(binary()) :: {:ok, [t() | group()]} | {:error, atom()}

Parses an address-list (To, Cc, Bcc, Reply-To): mailboxes and groups.

parse_mailbox(value)

@spec parse_mailbox(binary()) :: {:ok, t()} | {:error, atom()}

Parses exactly one mailbox (Sender).

parse_mailbox_list(value)

@spec parse_mailbox_list(binary()) :: {:ok, [t()]} | {:error, atom()}

Parses a mailbox-list (From): mailboxes only; a group is an error.