Rust

mime

Module

MIME: header fields, body encodings and the parts of a message.

Contents

3 submodules.

mime::encoding

Own page

Body encodings, chosen for the caller: quoted-printable for text and base64 for attachments.

use utopia_smtp::mime::encoding::base64;
Rust
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).

Rust
use utopia_smtp::mime::encoding;

assert_eq!(encoding::base64(b"hello"), b"aGVsbG8=\r\n");
use utopia_smtp::mime::encoding::boundary;
Rust
pub fn boundary() -> String

A 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;
Rust
pub fn is_ascii(value: &[u8]) -> bool

Whether every byte is ASCII (the empty string included).

Rust
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;
Rust
pub fn line_endings(value: &[u8]) -> Cow<'_, [u8]>

Every line ending, whatever it started as (CRLF, CR or LF), becomes CRLF.

Rust
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;
Rust
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.

Rust
use utopia_smtp::mime::encoding;

assert_eq!(encoding::quoted_printable("1 = 1\nhé".as_bytes()), b"1 =3D 1\r\nh=C3=A9");
Rust
pub const CHUNK: usize = 57

Base64 wraps at 76 characters, which is 57 bytes of input.

mime::header

Own page

Header 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;
Rust
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.

Rust
use utopia_smtp::mime::header;

assert_eq!(header::line("Subject", "Hellö")?, "Subject: =?UTF-8?B?SGVsbMO2?=\r\n");
use utopia_smtp::mime::header::phrase;
Rust
pub fn phrase(value: &str) -> String

A display name for use inside a structured value: quoted (with " and \ escaped) when ASCII, encoded words otherwise.

Rust
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 page

The MIME tree.

use utopia_smtp::mime::part::Chunks;
Rust
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.

Also implements Iterator.
use utopia_smtp::mime::part::Part;
Rust
pub struct Part

One node of the MIME tree: content, an attachment, or a boundary with children under it.

Rust
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");