eml_codec/lib.rs
1#![doc = include_str!("../README.md")]
2
3/// Parse and represent full emails as "parts" as defined by MIME (RFC 2046)
4pub mod part;
5
6/// Parse and represent IMF (Internet Message Format) headers (RFC 822, RFC 5322)
7pub mod imf;
8
9/// Parse and represent MIME headers (RFC 2045, RFC 2047)
10pub mod mime;
11
12/// MIME and IMF represent headers the same way: module contains their commong logic
13pub mod header;
14
15/// Low-level email-specific text-based representation for data
16pub mod text;
17
18/// Manipulate buffer of bytes
19mod pointers;
20
21use nom::{combinator::into, IResult};
22
23/// Parse a whole email including its (MIME) body
24///
25/// Returns the parsed content, but also the remaining bytes
26/// if the parser stopped before arriving to the end (for example
27/// due to a multipart delimiter).
28///
29/// # Arguments
30///
31/// * `input` - A buffer of bytes containing your full email
32///
33/// # Returns
34///
35/// * `rest` - The rest of the buffer, the part that is not parsed as the email ended before the
36/// end of the data
37/// * `msg` - The parsed message
38///
39/// # Examples
40///
41/// ```
42/// let input = br#"Date: 7 Mar 2023 08:00:00 +0200
43/// From: deuxfleurs@example.com
44/// To: someone_else@example.com
45/// Subject: An RFC 822 formatted message
46/// MIME-Version: 1.0
47/// Content-Type: text/plain; charset=us-ascii
48///
49/// This is the plain text body of the message. Note the blank line
50/// between the header information and the body of the message."#;
51///
52/// let (_, email) = eml_codec::parse_message(input).unwrap();
53/// println!(
54/// "{} raw message is:\n{}",
55/// email.imf.from[0].to_string(),
56/// String::from_utf8_lossy(email.child.as_text().unwrap().body),
57/// );
58/// ```
59pub fn parse_message(input: &[u8]) -> IResult<&[u8], part::composite::Message> {
60 into(part::composite::message(mime::MIME::<
61 mime::r#type::DeductibleMessage,
62 >::default()))(input)
63}
64
65/// Only extract the headers of the email that are part of the Internet Message Format spec
66///
67/// Emails headers contain MIME and IMF (Internet Message Format) headers.
68/// Sometimes you only need to know the recipient or the sender of an email,
69/// and are not interested in its content. In this case, you only need to parse the IMF
70/// fields and can ignore the MIME headers + the body. This is what this function does.
71///
72/// # Arguments
73///
74/// * `input` - A buffer of bytes containing either only the headers of your email or your full
75/// email (in both cases, the body will be ignored)
76///
77/// # Returns
78///
79/// * `rest` - The rest of the buffer, ie. the body of your email as raw bytes
80/// * `imf` - The parsed IMF headers of your email
81///
82/// # Examples
83///
84/// ```
85/// let input = br#"Date: 7 Mar 2023 08:00:00 +0200
86/// From: deuxfleurs@example.com
87/// To: someone_else@example.com
88/// Subject: An RFC 822 formatted message
89/// MIME-Version: 1.0
90/// Content-Type: text/plain; charset=us-ascii
91///
92/// This is the plain text body of the message. Note the blank line
93/// between the header information and the body of the message."#;
94///
95/// let (_, imf) = eml_codec::parse_imf(input).unwrap();
96/// println!(
97/// "{} just sent you an email with subject \"{}\"",
98/// imf.from[0].to_string(),
99/// imf.subject.unwrap().to_string(),
100/// );
101/// ```
102pub fn parse_imf(input: &[u8]) -> IResult<&[u8], imf::Imf> {
103 imf::imf(input)
104}