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.

extern crate email_format;

use email_format::Email;

fn main() {
  let mut email = Email::new(
      "myself@mydomain.com",  // "From:"
      "Wed, 05 Jan 2015 15:13:05 +1300" // "Date:"
  ).unwrap();
  email.set_sender("from_myself@mydomain.com").unwrap();
  email.set_reply_to("My Mailer <no-reply@mydomain.com>").unwrap();
  email.set_to("You <you@yourdomain.com>").unwrap();
  email.set_cc("Our Friend <friend@frienddomain.com>").unwrap();
  email.set_message_id("<id/20161128115731.29084.maelstrom@mydomain.com>").unwrap();
  email.set_subject("Hello Friend").unwrap();
  email.set_body("Good to hear from you.\r\n\
                  I wish you the best.\r\n\
                  \r\n\
                  Your Friend").unwrap();

  println!("{}", email);
}

This outputs:

Date:Wed, 05 Jan 2015 15:13:05 +1300
From:myself@mydomain.com
Sender:from_myself@mydomain.com
Reply-To:My Mailer <no-reply@mydomain.com>
To:You <you@yourdomain.com>
Cc:Our Friend <friend@frienddomain.com>
Message-ID:<id/20161128115731.29084.maelstrom@mydomain.com>
Subject:Hello Friend

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

Your Friend

On the other hand, the following will fail because the sender email address is invalid:

extern crate email_format;

use email_format::Email;

fn main() {
  let mut email = Email::new(
      "myself@mydomain.com",  // "From:"
      "Wed, 05 Jan 2015 15:13:05 +1300" // "Date:"
  ).unwrap();
  email.set_sender("from_myself@(mydomain.com)").unwrap();
}

Modules

rfc5322

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

Structs

Email

Traits

TryFrom

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