Crate email_format [] [src]


This crate allows you to construct email messages in a way that assures that they are compliant with relevant email standards (especially RFC 5322). Invalid data submitted will return a ParseError.

The main structure to work with is Email. It has many functions to set or add headers and to set the body. All of these will accept an &str or &[u8] argument and attempt to parse it. These setters return a Result<(), ParseError> as the parse may fail.


You can compose an email like this:

use email_format::Email;

let mut email = Email::new(
    "",  // "From:"
    "Wed, 5 Jan 2015 15:13:05 +1300" // "Date:"
email.set_reply_to("My Mailer <>").unwrap();
email.set_to("You <>").unwrap();
email.set_cc("Our Friend <>").unwrap();
email.set_subject("Hello Friend").unwrap();
email.set_body("Good to hear from you.\r\n\
                I wish you the best.\r\n\
                Your Friend").unwrap();

println!("{}", email);

This outputs:

Date:Wed, 5 Jan 2015 15:13:05 +1300
Reply-To:My Mailer <>
To:You <>
Cc:Our Friend <>
Subject:Hello Friend

Good to hear from you.
I wish you the best.

Your Friend


The following code will parse the input bytes (or panic if the parse failed), and leave any trailing bytes in the remainder, which should be checked to verify it is empty.

use email_format::Email;
use email_format::rfc5322::Parsable;

let input = "Date: Wed, 5 Jan 2015 15:13:05 +1300\r\n\
             My-Crazy-Field: this is my field\r\n\
             Subject: Hello Friend\r\n\
             Good to hear from you.\r\n\
             I wish you the best.\r\n\
             Your Friend".as_bytes();
 let (mut email, remainder) = Email::parse(&input).unwrap();
 assert_eq!(remainder.len(), 0);

Usage with lettre and/or mailstrom

If compiled with the lettre feature, you can generate a SimpleSendableEmail like this, and then use the lettre crate (or mailstrom) to send it.

Be careful when using this code, it's not being tested!
let simple_sendable_email = email.as_simple_sendable_email().unwrap();



This module contains nitty-gritty details about parsing, storage, and streaming an Email.





Attempt to construct Self via a conversion (borrowed from rust std)