#parser #email #rfc822 #mime #maildir


A simple parser for MIME e-mail messages

12 releases

0.6.3 Sep 6, 2018
0.6.2 May 24, 2018
0.6.1 Apr 4, 2018
0.6.0 Oct 14, 2017
0.2.0 Jun 22, 2016

#5 in Email

Download history 66/week @ 2018-06-14 25/week @ 2018-06-21 61/week @ 2018-06-28 168/week @ 2018-07-05 103/week @ 2018-07-12 111/week @ 2018-07-19 86/week @ 2018-07-26 129/week @ 2018-08-02 179/week @ 2018-08-09 130/week @ 2018-08-16 84/week @ 2018-08-23 93/week @ 2018-08-30 141/week @ 2018-09-06

458 downloads per month
Used in 3 crates

0BSD license



Build Status Crate

A simple parser for MIME email messages.


The primary entry point for this library is the following function:

    parse_mail(&[u8]) -> Result<ParsedMail, MailParseError>

This function takes the raw message data, including headers and body, and returns a structured object to more easily access pieces of the email message. There are other public functions that allow parsing smaller parts of the message as well; refer to the full documentation.

The library is designed to process real-world email data such as might be obtained by using the FETCH command on an IMAP server, or in a Maildir. As such, this library should successfully handle any valid MIME-formatted message, although it may not follow all the strict requirements in the various specifications that cover the format (predominantly IETF RFC 822 and IETF RFC 2045). As an example, this library accepts raw message data which uses \n (ASCII LF) as line delimiters rather than the RFC-mandated \r\n (ASCII CRLF) line delimiters.

Example usage

    use mailparse::*;
    let parsed = parse_mail(concat!(
            "Subject: This is a test email\n",
            "Content-Type: multipart/alternative; boundary=foobar\n",
            "Date: Sun, 02 Oct 2016 07:06:22 -0700 (PDT)\n",
            "Content-Type: text/plain; charset=utf-8\n",
            "Content-Transfer-Encoding: quoted-printable\n",
            "This is the plaintext version, in utf-8. Proof by Euro: =E2=82=AC\n",
            "Content-Type: text/html\n",
            "Content-Transfer-Encoding: base64\n",
            "PGh0bWw+PGJvZHk+VGhpcyBpcyB0aGUgPGI+SFRNTDwvYj4gdmVyc2lvbiwgaW4g \n",
            "dXMtYXNjaWkuIFByb29mIGJ5IEV1cm86ICZldXJvOzwvYm9keT48L2h0bWw+Cg== \n",
            "After the final boundary stuff gets ignored.\n").as_bytes())
    assert_eq!(parsed.headers.get_first_value("Subject").unwrap(), Some("This is a test email".to_string()));
    assert_eq!(parsed.subparts.len(), 2);
    assert_eq!(parsed.subparts[0].get_body().unwrap(), "This is the plaintext version, in utf-8. Proof by Euro: \u{20AC}");
    assert_eq!(parsed.subparts[1].headers[1].get_value().unwrap(), "base64");
    assert_eq!(parsed.subparts[1].ctype.mimetype, "text/html");
    assert_eq!(dateparse(parsed.headers.get_first_value("Date").unwrap().unwrap().as_str()).unwrap(), 1475417182);


See the rustdoc at https://docs.rs/mailparse/.

Other notes

This is written by a newbie Rust programmer, so code may be non-idiomatic or suboptimal. Pull requests are welcome!


~35K SLoC