mime
ModuleMIME: header fields, body encodings and the parts of a message.
Contents
3 submodules.
mime::encoding
Own pageBody encodings, chosen for the caller: quoted-printable for text and base64 for attachments.
use utopia_smtp::mime::encoding::base64;pub fn base64(value: &[u8]) -> Vec<u8>Base64 in lines of 76 characters, each ended by CRLF (the last one too, so a part always ends on its own line).
use utopia_smtp::mime::encoding;
assert_eq!(encoding::base64(b"hello"), b"aGVsbG8=\r\n");use utopia_smtp::mime::encoding::boundary;pub fn boundary() -> StringA fresh multipart boundary: =_ and 32 random hex digits. It needs no
quoting and cannot appear in a base64 body.
use utopia_smtp::mime::encoding::is_ascii;pub fn is_ascii(value: &[u8]) -> boolWhether every byte is ASCII (the empty string included).
use utopia_smtp::mime::encoding;
assert!(encoding::is_ascii(b"plain text"));
assert!(!encoding::is_ascii("héllo".as_bytes()));use utopia_smtp::mime::encoding::line_endings;pub fn line_endings(value: &[u8]) -> Cow<'_, [u8]>Every line ending, whatever it started as (CRLF, CR or LF), becomes CRLF.
use utopia_smtp::mime::encoding;
assert_eq!(&*encoding::line_endings(b"a\n\rb"), b"a\r\n\r\nb");use utopia_smtp::mime::encoding::quoted_printable;pub fn quoted_printable(value: &[u8]) -> Vec<u8>Quoted-printable (RFC 2045 section 6.7) after line_endings, with
lines of at most 76 characters.
use utopia_smtp::mime::encoding;
assert_eq!(encoding::quoted_printable("1 = 1\nhé".as_bytes()), b"1 =3D 1\r\nh=C3=A9");pub const CHUNK: usize = 57Base64 wraps at 76 characters, which is 57 bytes of input.
mime::header
Own pageHeader fields, from a name and a value to bytes a server will accept: encoded words for anything outside ASCII, split so none exceeds its limit, and folding so no line runs long.
use utopia_smtp::mime::header::line;pub fn line(name: &str, value: &str) -> Result<String, Error>One field, folded and terminated by CRLF. A value outside ASCII becomes RFC 2047 encoded words. The value must already be free of line endings.
A space-free run longer than 998 octets cannot be folded and is an
Error::InvalidArgument.
use utopia_smtp::mime::header;
assert_eq!(header::line("Subject", "Hellö")?, "Subject: =?UTF-8?B?SGVsbMO2?=\r\n");use utopia_smtp::mime::header::phrase;pub fn phrase(value: &str) -> StringA display name for use inside a structured value: quoted (with " and
\ escaped) when ASCII, encoded words otherwise.
use utopia_smtp::mime::header;
assert_eq!(header::phrase("Jane Doe"), "\"Jane Doe\"");
assert_eq!(header::phrase("Jäne"), "=?UTF-8?B?SsOkbmU=?=");mime::part
Own pageThe MIME tree.
use utopia_smtp::mime::part::Chunks;pub struct Chunks<'a>The pieces a message or part is written in, read lazily: a file is opened and read block by block as the pieces are taken.
Iterator.use utopia_smtp::mime::part::Part;pub struct PartOne node of the MIME tree: content, an attachment, or a boundary with children under it.
use utopia_smtp::mime::Part;
let part = Part::text("text/plain", "héllo".as_bytes());
assert_eq!(
part.head()?,
"Content-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n",
);
assert_eq!(part.to_bytes()?, b"h=C3=A9llo");