Skip to main content

MessageParser

Struct MessageParser 

Source
pub struct MessageParser { /* private fields */ }
Expand description

RFC 5322 message parser and its configuration.

Build a parser once and reuse it; it is Send + Sync. The default configuration parses the header fields that 0.11 parsed into structured values the same way and keeps every other field raw.

use mail_parser::{HeaderForm, HeaderName, MessageParser};

let parser = MessageParser::new()
    .header(HeaderName::Received, HeaderForm::Raw)
    .header("X-Sender", HeaderForm::Addresses)
    .max_parts(1_000);
let message = parser
    .parse(b"X-Sender: <sender@example.com>\r\n\r\nbody")
    .expect("message");
let sender = message.headers().value("x-sender").and_then(|value| value.as_address());
let address = sender.and_then(|list| list.first()).and_then(|mailbox| mailbox.address());
assert_eq!(address, Some("sender@example.com"));

Implementations§

Source§

impl MessageParser

Source

pub fn new() -> Self

A parser with the default header table: every header 0.11 parsed into a structured value is parsed the same way, every other header is kept raw. Limits: nesting depth 64, 10,000 parts, 3 levels of transfer-encoded nested messages.

Source

pub fn header( self, name: impl Into<HeaderName<'static>>, form: HeaderForm, ) -> Self

Parses the header name as form, in place of its default form. Content-Type, Content-Disposition and Content-Transfer-Encoding always keep their MIME form. Names compare ASCII case-insensitively, and a HeaderName::Other spelled like a known name configures that name; any other name is added to a short list checked for every unknown name.

Source

pub fn unknown_headers(self, form: HeaderForm) -> Self

The form used for header names that are neither known nor registered with MessageParser::header; HeaderForm::Raw by default.

It also applies to the vendor names that are known since 1.0 but were unknown to 0.11 (from HeaderName::XOriginalTo on: X-Mailer, User-Agent, X-Priority, the X-MS-Exchange, X-Google and X-GitHub fields, …), except those registered with header, whatever the order of the calls.

Source

pub fn max_depth(self, depth: usize) -> Self

Maximum number of nested containers (multiparts and messages), the root message included: with the default of 64, a part can be nested in at most 63 containers below the root message.

Source

pub fn max_parts(self, parts: usize) -> Self

Maximum number of parts; the content past the limit stays unparsed in the part flagged PartFlags::LIMIT_REACHED.

Source

pub fn max_encoded_nesting(self, levels: usize) -> Self

Maximum nesting of transfer-encoded message/rfc822 parts that are decoded and parsed; 0 leaves them as binary parts.

Source

pub fn parse<'x>( &self, raw: &'x (impl AsRef<[u8]> + ?Sized), ) -> Option<Message<'x>>

Parses a message that borrows raw. Returns None when the input has no header and no blank line, or is 4 GiB or larger.

Source

pub fn parse_owned(&self, raw: Vec<u8>) -> Option<Message<'static>>

Parses a message and takes ownership of its bytes, giving a Message<'static> that can be moved to another task.

Source

pub fn parse_headers<'x>( &self, raw: &'x (impl AsRef<[u8]> + ?Sized), ) -> Option<Message<'x>>

Parses only the root header block, for callers that do not need the MIME structure (DKIM verification, header indexing).

The message has a single part, the root part, which covers the whole body and keeps the kind its Content-Type gives (a multipart root has no children). No part is in a body list or in the attachments: crate::MessageRef::has_attachments is false, and a root that is not a multipart is reported by crate::MessageRef::other_parts.

Source

pub fn parse_with<'x>( &self, raw: &'x (impl AsRef<[u8]> + ?Sized), buffers: &mut MessageBuffers, ) -> Option<Message<'x>>

Parses a message into the storage held by buffers, which a previous message gave back with Message::into_buffers; once the storage fits the messages, parsing allocates nothing. See MessageBuffers for a loop.

The message takes the storage out of buffers, which stay usable: when the input does not parse, the storage is dropped and the next parse allocates.

Trait Implementations§

Source§

impl Clone for MessageParser

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MessageParser

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for MessageParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for MessageParser

Source§

impl PartialEq for MessageParser

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MessageParser

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.