netpkt/imf.rs#
Source: lib/netpkt/imf.rs
imf -- Internet messages (RFC 5322) with the part of MIME (RFC 2045, 2046, 2047) that plain
text mail needs: header fields with folding, addresses with display names, the Date form,
Message-ID, a text/plain body in UTF-8 as 7bit, 8bit or quoted-printable, multipart bodies
(a bounce's multipart/report of RFC 3464 and 6522) and base64
Field { name, value } one header field; the value unfolded (fold() writes it again)
Message { fields, body } parse(bytes) (CRLF or LF; the header ends at the first empty
line), get(name) / get_all / set / add / prepend / remove,
to_bytes() (CRLF, every field folded at 78 columns), text()
(the body as text: transfer encoding, charset and multipart
undone), subject() / from() / to() / cc() / date()
Mailbox { name, addr } parse_mailbox("Anna Muster <anna@a.lab>"), parse_list("a@x, B
<b@y>"), to_header() (a display name quoted or as an encoded
word); addr_ok(addr), split_addr(addr) -> (local, domain),
split_list(text): the parts between the commas, as typed
params(value) "text/plain; charset=UTF-8" -> ("text/plain", [("charset",
"UTF-8")]); param(value, name)
date(unix) / parse_date(text) the Date form of 3.3 ("Thu, 01 Jan 2026 00:00:05 +0000"),
read back to seconds since 1970 (UTC, any zone it names)
message_id(left, domain) "<left@domain>"
encode_word / decode_words RFC 2047 encoded words (UTF-8, Q and B; ISO-8859-1 read too)
qp_encode / qp_decode quoted-printable of RFC 2045 6.7 (soft line breaks at 76)
b64_encode / b64_lines / b64_decode base64 of RFC 4648 (MIME lines of 76, padding)
Draft { from, to, cc, subject, date, id, in_reply_to, references, text }
to_bytes(eight_bit): the message a mail program sends
(MIME-Version 1.0, text/plain in UTF-8; 7bit for ASCII, 8bit
when the server takes 8BITMIME, else quoted-printable)
fold(name, value) "Name: value\r\n" with continuation lines that start with a
space (2.2.3); CR and LF of a value become spaces, so no value
adds a field or ends the header
Lines of a header field are at most 78 characters where there is whitespace to break at
(2.1.1); a word longer than that stays whole. Display names and subjects with characters
outside ASCII travel as encoded words, so a header stays 7-bit. Addresses are not
normalised: the local part keeps its case, the domain is compared without it by the callers.
This is no full MIME implementation: nested multiparts are searched for their first text
part, other parts (attachments) are named but not decoded.
struct Fielda header fieldname: Stringvalue: String-- unfolded, without the space after the colon
struct Messagea message: its header fields in order and the body bytesfields: Vec<Field>body: Vec<u8>
impl Message
fn new() -> Messagefn parse(b: &[u8]) -> Messagea message as it travels or lies in a mailbox: header lines (folded lines unfolded), the empty line, the body; a line without a colon in the header is left outfn get(&self, name: &str) -> Option<&str>the first field of a name (any case)fn get_all<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str> + 'aevery field of a name, in orderfn set(&mut self, name: &str, value: &str)replaces the first field of a name (or adds it at the end)fn add(&mut self, name: &str, value: &str)fn prepend(&mut self, name: &str, value: &str)a field at the top (trace fields: Received, Return-Path)fn remove(&mut self, name: &str)fn header_bytes(&self) -> Vec<u8>the header as it travels (CRLF, folded), without the empty line; a field whose name is empty or holds other than printable characters but the colon is left outfn to_bytes(&self) -> Vec<u8>header, empty line, body (the body as it is)fn subject(&self) -> Stringthe subject with its encoded words decodedfn from(&self) -> Option<Mailbox>the first mailbox of Fromfn to(&self) -> Vec<Mailbox>fn cc(&self) -> Vec<Mailbox>fn date(&self) -> Option<i64>the Date field in seconds since 1970fn text(&self) -> Stringthe body as text (line ends "\n"): the first text/plain part of a multipart, its transfer encoding and charset undonefn split_multipart(text: &[u8], boundary: &str) -> Vec<Vec<u8>>the parts of a multipart body between its boundary lines (RFC 2046 5.1.1)fn fold(name: &str, value: &str) -> String"Name: value\r\n" folded at whitespace so that no line is longer than 78 characters where that is possible. No text starts a field or the body of its own: the name keeps only its printable characters but the colon, CR and LF in the value become spaces, NUL is left outfn unfold(s: &str) -> Stringremoves the line ends of folded lines (CRLF or LF before whitespace)fn params(value: &str) -> (String, Vec<(String, String)>)the value and parameters of a field like Content-Type: ("text/plain", [("charset", "UTF-8")]); names in lower case, quotes removedfn param(value: &str, name: &str) -> Option<String>one parameter of such a fieldfn split_list(s: &str) -> Vec<&str>the parts of an address list between its commas (not those inside quotes, angle brackets or comments), as typedstruct Mailboxan address with its display name (empty when there is none)name: Stringaddr: String
impl Mailbox
fn new(name: &str, addr: &str) -> Mailboxfn to_header(&self) -> Stringas a header writes it: "addr", "Name <addr>", "\"Muster, Anna\" <addr>" or an encoded word for a name outside ASCIIfn shown(&self) -> &strthe name when there is one, else the addressfn parse_mailbox(s: &str) -> Option<Mailbox>a mailbox: "Anna <anna@a.lab>", "\"Muster, Anna\" <anna@a.lab>", "anna@a.lab", "anna@a.lab (Anna)"; None when no address can be readfn parse_list(s: &str) -> Vec<Mailbox>the mailboxes of an address list ("a@x, B <b@y>"; a group "Team: a@x, b@y;" gives its members, "undisclosed-recipients:;" none)fn addr_ok(a: &str) -> boolthe address has the form local@domain: a dot-atom or a quoted local part, a domain of labels (letters, digits, hyphens) or an address literal in bracketsfn split_addr(a: &str) -> Option<(&str, &str)>(local part, domain) at the last '@'fn date(unix: i64) -> String"Thu, 01 Jan 2026 00:00:05 +0000" of seconds since 1970 (UTC)fn parse_date(s: &str) -> Option<i64>the seconds since 1970 of a Date field: the day of the week is optional, the zone may be +hhmm / -hhmm or one of the obsolete names (UT, GMT, Z, EST ...)fn message_id(left: &str, domain: &str) -> Stringa Message-ID: "<left@domain>"fn encode_word(text: &str) -> Stringtext for a header: as it is in ASCII, else encoded words "=?UTF-8?Q?...?=" of at most 75 characters each (separated by a space, a character never split)fn decode_words(s: &str) -> Stringa header value with its encoded words decoded (whitespace between two encoded words goes)fn qp_encode(text: &[u8]) -> Vec<u8>quoted-printable (RFC 2045 6.7): lines end in CRLF, a line longer than 76 breaks softly ("=" at its end), "=", control and 8-bit bytes as "=XX", spaces at a line end toofn qp_decode(b: &[u8]) -> Vec<u8>undoes quoted-printable: "=XX" bytes, soft line breaks, whitespace at line endsfn b64_encode(data: &[u8]) -> Stringbase64 with padding, one linefn b64_lines(data: &[u8]) -> Stringbase64 in MIME lines of 76 characters with CRLFfn b64_decode(s: &[u8]) -> Option<Vec<u8>>base64 back to bytes; whitespace and line ends are skipped, None for other charactersstruct Draftwhat a mail program puts into a new messagefrom: Mailboxto: Vec<Mailbox>cc: Vec<Mailbox>subject: Stringdate: i64-- seconds since 1970id: String-- the Message-ID with its angle bracketsin_reply_to: String-- the Message-ID of the message answered (empty: none)references: String-- the References of a reply: the ids before, separated by spacestext: String-- the text with "\n" line ends
impl Draft
fn to_bytes(&self, eight_bit: bool) -> Vec<u8>the message (CRLF): 7bit when the text is ASCII, else 8bit when the server takes 8BITMIME, else quoted-printable; lines over 998 bytes always go quoted-printablefn crlf(b: &[u8]) -> Vec<u8>text with "\n" (or CRLF) line ends as CRLF linesfn lf(b: &[u8]) -> Vec<u8>CRLF line ends as "\n" (a mailbox file keeps its messages that way)