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 addressvictim@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
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
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.
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.
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.