Struct mailparse::ParsedMail

source ·
pub struct ParsedMail<'a> {
    pub raw_bytes: &'a [u8],
    pub headers: Vec<MailHeader<'a>>,
    pub ctype: ParsedContentType,
    pub subparts: Vec<ParsedMail<'a>>,
    /* private fields */
}
Expand description

Struct that holds the structured representation of the message. Note that since MIME allows for nested multipart messages, a tree-like structure is necessary to represent it properly. This struct accomplishes that by holding a vector of other ParsedMail structures for the subparts.

Fields§

§raw_bytes: &'a [u8]

The raw bytes that make up this message (or subpart).

§headers: Vec<MailHeader<'a>>

The headers for the message (or message subpart).

§ctype: ParsedContentType

The Content-Type information for the message (or message subpart).

§subparts: Vec<ParsedMail<'a>>

The subparts of this message or subpart. This vector is only non-empty if ctype.mimetype starts with “multipart/”.

Implementations§

source§

impl<'a> ParsedMail<'a>

source

pub fn get_body(&self) -> Result<String, MailParseError>

Get the body of the message as a Rust string. This function tries to unapply the Content-Transfer-Encoding if there is one, and then converts the result into a Rust UTF-8 string using the charset in the Content-Type (or “us-ascii” if the charset was missing or not recognized). Note that in some cases the body may be binary data that doesn’t make sense as a Rust string - it is up to the caller to handle those cases gracefully. These cases may occur in particular when the body is of a “binary” Content-Transfer-Encoding (i.e. where get_body_encoded() returns a Body::Binary variant) but may also occur in other cases because of the messiness of the real world and non-compliant mail implementations.

Examples
    use mailparse::parse_mail;
    let p = parse_mail(concat!(
            "Subject: test\n",
            "\n",
            "This is the body").as_bytes())
        .unwrap();
    assert_eq!(p.get_body().unwrap(), "This is the body");
source

pub fn get_body_raw(&self) -> Result<Vec<u8>, MailParseError>

Get the body of the message as a Rust Vec. This function tries to unapply the Content-Transfer-Encoding if there is one, but won’t do any charset decoding.

Examples
    use mailparse::parse_mail;
    let p = parse_mail(concat!(
            "Subject: test\n",
            "\n",
            "This is the body").as_bytes())
        .unwrap();
    assert_eq!(p.get_body_raw().unwrap(), b"This is the body");
source

pub fn get_body_encoded(&'a self) -> Body<'a>

Get the body of the message. This function returns the original body without attempting to unapply the Content-Transfer-Encoding. The returned object contains information that allows the caller to control decoding as desired.

Examples
    use mailparse::parse_mail;
    use mailparse::body::Body;

    let mail = parse_mail(b"Content-Transfer-Encoding: base64\r\n\r\naGVsbG 8gd\r\n29ybGQ=").unwrap();

    match mail.get_body_encoded() {
        Body::Base64(body) => {
            assert_eq!(body.get_raw(), b"aGVsbG 8gd\r\n29ybGQ=");
            assert_eq!(body.get_decoded().unwrap(), b"hello world");
            assert_eq!(body.get_decoded_as_string().unwrap(), "hello world");
        },
        _ => assert!(false),
    };


    // An email whose body encoding is not known upfront
    let another_mail = parse_mail(b"").unwrap();

    match another_mail.get_body_encoded() {
        Body::Base64(body) | Body::QuotedPrintable(body) => {
            println!("mail body encoded: {:?}", body.get_raw());
            println!("mail body decoded: {:?}", body.get_decoded().unwrap());
            println!("mail body decoded as string: {}", body.get_decoded_as_string().unwrap());
        },
        Body::SevenBit(body) | Body::EightBit(body) => {
            println!("mail body: {:?}", body.get_raw());
            println!("mail body as string: {}", body.get_as_string().unwrap());
        },
        Body::Binary(body) => {
            println!("mail body binary: {:?}", body.get_raw());
        }
    }
source

pub fn get_headers(&'a self) -> Headers<'a>

Returns a struct that wraps the headers for this message. The struct provides utility methods to read the individual headers.

source

pub fn get_content_disposition(&self) -> ParsedContentDisposition

Returns a struct containing a parsed representation of the Content-Disposition header. The first header with this name is used, if there are multiple. See the parse_content_disposition method documentation for more details on the semantics of the returned object.

source

pub fn parts(&'a self) -> PartsIterator<'a>

Returns a depth-first pre-order traversal of the subparts of this ParsedMail instance. The first item returned will be this ParsedMail itself.

Trait Implementations§

source§

impl<'a> Debug for ParsedMail<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for ParsedMail<'a>

§

impl<'a> Send for ParsedMail<'a>

§

impl<'a> Sync for ParsedMail<'a>

§

impl<'a> Unpin for ParsedMail<'a>

§

impl<'a> UnwindSafe for ParsedMail<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.