Building MIME e-mail messages in Rust

Stalwart Labs
2 min readJan 25, 2022

Back in November we released mail-parser, a Rust library to parse MIME e-mail messages of any complexity. Today we are proud to announce the release of mail-builder, a simple yet powerful library to build RFC5322 e-mail messages with MIME support.

Using mail-builder is straightforward:

    // Build a simple text message with a single attachment
let mut message = MessageBuilder::new();
message.from(("John Doe", "john@doe.com"));
message.to("jane@doe.com");
message.subject("Hello, world!");
message.text_body("Message contents go here.");
message.binary_attachment("image/png", "image.png", &[1, 2, 3, 4]);
// Write message to memory
let mut output = Vec::new();
message.write_to(&mut output).unwrap();

More complex messages with grouped addresses, inline parts and multipart/alternative sections can also be easily built:

    // Build a multipart message with text and HTML bodies,
// inline parts and attachments.
let mut message = MessageBuilder::new();
message.from(("John Doe", "john@doe.com"));
// To recipients
message.to(vec![
("Antoine de Saint-Exupéry", "antoine@exupery.com"),
("안녕하세요 세계", "test@test.com"),
("Xin chào", "addr@addr.com"),
]);
// BCC recipients using grouped addresses
message.bcc(vec![
(
"My Group",
vec![
("ASCII name", "addr1@addr7.com"),
("ハロー・ワールド", "addr2@addr6.com"),
("áéíóú", "addr3@addr5.com"),
("Γειά σου Κόσμε", "addr4@addr4.com"),
],
),
(
"Another Group",
vec![
("שלום עולם", "addr5@addr3.com"),
("ñandú come ñoquis", "addr6@addr2.com"),
("Recipient", "addr7@addr1.com"),
],
),
]);
// Set RFC and custom headers
message.subject("Testing multipart messages");
message.in_reply_to(vec!["message-id-1", "message-id-2"]);
message.header("List-Archive", URL::new("http://example.com/archive"));
// Set HTML and plain text bodies
message.text_body("This is the text body!\n");
message.html_body("<p>HTML body with <img src=\"cid:my-image\"/>!</p>");
// Include an embedded image as an inline part
message.binary_inline("image/png", "cid:my-image", &[0, 1, 2, 3, 4, 5]);
// Add a text and a binary attachment
message.text_attachment("text/plain", "my fíle.txt", "Attachment contents go here.");
message.binary_attachment(
"text/plain",
"ハロー・ワールド",
b"Binary contents go here.",
);
// Write the message to a file
message
.write_to(File::create("message.eml").unwrap())
.unwrap();

Nested MIME body structures can be created using the body method:

// Build a nested multipart message
let mut message = MessageBuilder::new();
message.from(Address::new_address("John Doe".into(), "john@doe.com"));
message.to(Address::new_address("Jane Doe".into(), "jane@doe.com"));
message.subject("Nested multipart message");
// Define the nested MIME body structure
message.body(MimePart::new_multipart(
"multipart/mixed",
vec![
MimePart::new_text("Part A contents go here...").inline(),
MimePart::new_multipart(
"multipart/mixed",
vec![
MimePart::new_multipart(
"multipart/alternative",
vec![
MimePart::new_multipart(
"multipart/mixed",
vec![
MimePart::new_text("Part B contents go here...").inline(),
MimePart::new_binary(
"image/jpeg",
"Part C contents go here...".as_bytes(),
)
.inline(),
MimePart::new_text("Part D contents go here...").inline(),
],
),
MimePart::new_multipart(
"multipart/related",
vec![
MimePart::new_html("Part E contents go here...").inline(),
MimePart::new_binary(
"image/jpeg",
"Part F contents go here...".as_bytes(),
),
],
),
],
),
MimePart::new_binary("image/jpeg", "Part G contents go here...".as_bytes())
.attachment("image_G.jpg"),
MimePart::new_binary(
"application/x-excel",
"Part H contents go here...".as_bytes(),
),
MimePart::new_binary(
"x-message/rfc822",
"Part J contents go here...".as_bytes(),
),
],
),
MimePart::new_text("Part K contents go here...").inline(),
],
));
// Write the message to a file
message
.write_to(File::create("nested-message.eml").unwrap())
.unwrap();

The library is available at https://crates.io/crates/mail-builder and the documentation at https://docs.rs/mail-builder.

--

--

Stalwart Labs

Secure and modern all-in-one mail server (JMAP, IMAP, SMTP)