mail-builder is a flexible e-mail builder library written in Rust that generates RFC5322 compliant e-mail messages

Overview

mail-builder

crates.io build docs.rs crates.io

mail-builder is a flexible e-mail builder library written in Rust that generates RFC5322 compliant e-mail messages. The library has full MIME support and automatically selects the most optimal encoding for each message body part.

Building e-mail messages is straightforward:

    // Build a simple text message with a single attachment
    let mut message = MessageBuilder::new();
    message.from(("John Doe", "[email protected]").into());
    message.to("[email protected]".into());
    message.subject("Hello, world!".into());
    message.text_body("Text body 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:

HTML body with !

"); // Include an embedded image as an inline part message.binary_inline("image/png", "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();">
    // Build a multipart message with text and HTML bodies,
    // inline parts and attachments.
    let mut message = MessageBuilder::new();
    message.from(("John Doe", "[email protected]").into());

    // To recipients
    message.to(vec![
        ("Antoine de Saint-Exupéry", "[email protected]").into(),
        ("안녕하세요 세계", "[email protected]").into(),
        ("Xin chào", "[email protected]").into(),
    ]
    .into());

    // BCC recipients using grouped addresses
    message.bcc(
        vec![
            (
                "My Group",
                vec![
                    ("ASCII name", "[email protected]").into(),
                    ("ハロー・ワールド", "[email protected]").into(),
                    ("áéíóú", "[email protected]").into(),
                    ("Γειά σου Κόσμε", "[email protected]").into(),
                ],
            )
                .into(),
            (
                "Another Group",
                vec![
                    ("שלום עולם", "[email protected]").into(),
                    ("ñandú come ñoquis", "[email protected]").into(),
                    "[email protected]".into(),
                ],
            )
                .into(),
        ]
        .into(),
    );

    // Set RFC and custom headers
    message.subject("Testing multipart messages".into());
    message.in_reply_to(vec!["message-id-1", "message-id-2"].into());
    message.header(
        "List-Archive",
        URL::new("http://example.com/archive").into(),
    );

    // Set HTML and plain text bodies
    message.text_body("This is the text body!\n");
    message.html_body("

HTML body with \"my-image\"/>!

"
); // Include an embedded image as an inline part message.binary_inline("image/png", "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(), "[email protected]"));
    message.to(Address::new_address("Jane Doe".into(), "[email protected]"));
    message.subject("Nested multipart message".into());

    // 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();

Please note that this library does not support parsing e-mail messages as this functionality is provided separately by the mail-parser crate.

Testing

To run the testsuite:

 $ cargo test --all-features

or, to run the testsuite with MIRI:

 $ cargo +nightly miri test --all-features

License

Licensed under either of

at your option.

Copyright

Copyright (C) 2020-2022, Stalwart Labs, Minter Ltd.

See COPYING for the license.

You might also like...
Implementation of mjml in rust

MRML Introduction This project is a reimplementation of the nice MJML markup language in Rust. How to use it use mrml; fn main() { match mrml::to

Rust implementation of catapulte email sender
Rust implementation of catapulte email sender

Catapulte What is catapulte? Catapulte is an open source mailer you can host yourself. You can use it to quickly catapult your transactionnal emails t

A rewrite of the server side parts of emersion/go-smtp package into rust.

rust-smtp-server A rust smtp server library. It's mainly a rewrite of the server side parts of the emersion/go-smtp library. Features Usage Add this t

Rust-idiomatic, compliant, flexible and performant BIP21 crate

Rust implementation of BIP21 Rust-idiomatic, compliant, flexible and performant BIP21 crate. About Important: while lot of work went into polishing th

turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's
turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's

turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's `gpt-3.5-turbo` language model. It is easy to use and a cost-effective way to keep git commit history at a higher quality, helping developers stay on track with their work.

A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages.

uy A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages. Usage uy not only stores the unit of a v

tidy-builder is a builder generator that is compile-time correct.
tidy-builder is a builder generator that is compile-time correct.

The Builder derive macro creates a compile-time correct builder which means that it only allows you to build the given struct if and only if you provi

Easy to use, extendable, OCI-compliant container runtime written in pure Rust
Easy to use, extendable, OCI-compliant container runtime written in pure Rust

PURA - Lightweight & OCI-compliant container runtime Pura is an experimental Linux container runtime written in pure and dependency-minimal Rust. The

Fast, simple and REST compliant file-server with public/private key authentication written in Rust

stormi Stormi is a fast and simple file-server with public/private key authentication How does it work? Stormi accepts multipart/form-data form with m

📜 A pci.ids-compliant library for getting information about available PCI devices.

aparato A pci.ids-compliant library for getting information about available PCI devices. Usage Add the following to your project's Cargo.toml file: ap

A library to quickly create OAuth2.1 compliant servers from scratch.

oauth21-server A library to easily create an OAuth 2.1 compliant authorization server. The motivation to develop this library comes from the fact that

A static mail HTML archive for the 21st century, written in Rust

🦀 Crabmail 🦀 self-hosted / github mirror A static mail HTML archive for the 21st century, written in Rust. Includes helpful "modern" features that e

A mail suite written in rust meant to be easy to use.

Erooster A mail suite written in rust meant to be easy to use. Getting started Currently the setup is quite rough. You need some certificates for your

Rust library to parse mail files

mailparse A simple parser for MIME email messages. API The primary entry point for this library is the following function: parse_mail(&[u8]) - Re

Fast and robust e-mail parsing library for Rust

mail-parser mail-parser is an e-mail parsing library written in Rust that fully conforms to the Internet Message Format standard (RFC 5322), the Multi

E-mail delivery library for Rust with DKIM support

mail-send mail-send is a Rust library to build, sign and send e-mail messages via SMTP. It includes the following features: Generates e-mail messages

Modern, lightweight & standard-compliant bitcoin wallet runtime & cli without rust-bitcoin dependencies

Bitcoin protocol command-line wallet & tools Modern, minimalistic & standard-compliant cold wallet from LNP/BP Standards Association. Contributing Con

A WHATWG-compliant HTML5 tokenizer and tag soup parser

html5gum html5gum is a WHATWG-compliant HTML tokenizer. use std::fmt::Write; use html5gum::{Tokenizer, Token}; let html = "title hello world/tit

A solana program designed to mint Metaplex compliant NFTs.

Solana Minter My program used to mint Amoebits & Amoebit Minis. I wrote it from scratch using the hello-world program as an example & base. Features C

Comments
  • Cut down on rand dependency

    Cut down on rand dependency

    Unless I am mistaken or don't understand the RFCs, message boundaries don't need high quality RNG. I believe you can replace with https://git.alexwennerberg.com/misc/file/rand.rs.html or a simpler non-cryptographic rng library

    opened by alexwennerberg 4
  • [Request] New version

    [Request] New version

    Hello @mdecimus.

    First of all thank you for your work on this library.

    I was trying to use this lib at the 0.1.2 version but i found too the bug you fixed here. Do you think that a new version of this lib could be available soon?

    Thank you in advance.

    opened by cottinisimone 3
  • support format=flowed?

    support format=flowed?

    1. I'm not sure whether this library should assume that a trailing space means that it should use quoted-printable instead of utf-7 -- what's the reasoning behind this?
    2. It would be nice to have a way to set format=flowed -- I put together some commits on https://github.com/alexwennerberg/mail-builder but I'm not sure if it's the best way to do it or something you want to support in this library
    opened by alexwennerberg 3
  • support 8-bit content encoding?

    support 8-bit content encoding?

    I'm not super familiar with email specs, but is there any reason why this library doesn't support 8-bit content-encoding? Is it not well-supported by clients and servers?

    https://datatracker.ietf.org/doc/html/rfc6152

    opened by alexwennerberg 1
Releases(0.2.4)
Owner
Stalwart Labs
We build distributed applications in Rust.
Stalwart Labs
Rust library to parse mail files

mailparse A simple parser for MIME email messages. API The primary entry point for this library is the following function: parse_mail(&[u8]) -> Re

Kartikaya Gupta (kats) 150 Dec 27, 2022
Fast and robust e-mail parsing library for Rust

mail-parser mail-parser is an e-mail parsing library written in Rust that fully conforms to the Internet Message Format standard (RFC 5322), the Multi

Stalwart Labs 158 Jan 1, 2023
E-mail delivery library for Rust with DKIM support

mail-send mail-send is a Rust library to build, sign and send e-mail messages via SMTP. It includes the following features: Generates e-mail messages

Stalwart Labs 165 Oct 23, 2023
An ESMTP server library written in Rust.

rs-smtp An ESMTP server library written in Rust. Features ESMTP client & server implementing RFC 5321 Support for SMTP AUTH and PIPELINING UTF-8 suppo

DUNEF 3 Apr 15, 2023
📫Himalaya: CLI email client written in Rust.

??Himalaya: CLI email client written in Rust.

Clément DOUIN 2.1k Jan 7, 2023
Check if an email address exists without sending any email, written in Rust.

Check if an email address exists without sending any email, written in Rust.

Reacher 3.5k Dec 31, 2022
Unofficial Rust library for the SendGrid API

sendgrid-rs Unofficial Rust library for the SendGrid API. This crate requires Rust 1.15 or higher as it uses a crate that has a custom derive implemen

Garrett Squire 88 Dec 27, 2022
a mailer library for Rust

lettre A mailer library for Rust NOTE: this readme refers to the 0.10 version of lettre, which is still being worked on. The master branch and the alp

lettre 1.3k Jan 4, 2023
A small unofficial library to send emails using Sendgrid.

sendgrid_thin A thin wrapper around the SendGrid V3 API. It does not use the crate tokio or hyper and is therefore very lightweight and do not interfe

Reinaldo Rozato Junior 3 Nov 17, 2022
新しい IMAP client in Rust

新しい IMAP client 新しい (atarashii/new) IMAP client in Rust. It supports plain and secure connections. In progress It's under development... Usage Put thi

Alex Maslakov 39 Sep 13, 2020