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
impl MessageParser
Sourcepub fn new() -> Self
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.
Sourcepub fn header(
self,
name: impl Into<HeaderName<'static>>,
form: HeaderForm,
) -> Self
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.
Sourcepub fn unknown_headers(self, form: HeaderForm) -> Self
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.
Sourcepub fn max_depth(self, depth: usize) -> Self
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.
Sourcepub fn max_parts(self, parts: usize) -> Self
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.
Sourcepub fn max_encoded_nesting(self, levels: usize) -> Self
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.
Sourcepub fn parse<'x>(
&self,
raw: &'x (impl AsRef<[u8]> + ?Sized),
) -> Option<Message<'x>>
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.
Sourcepub fn parse_owned(&self, raw: Vec<u8>) -> Option<Message<'static>>
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.
Sourcepub fn parse_headers<'x>(
&self,
raw: &'x (impl AsRef<[u8]> + ?Sized),
) -> Option<Message<'x>>
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.
Sourcepub fn parse_with<'x>(
&self,
raw: &'x (impl AsRef<[u8]> + ?Sized),
buffers: &mut MessageBuffers,
) -> Option<Message<'x>>
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.