Skip to main content

Message

Struct Message 

Source
pub struct Message<'x> { /* private fields */ }
Expand description

A parsed message: the raw bytes and a store of every nested message, MIME part and header field, read through small Copy views such as MessageRef, MessagePart and crate::Header. Bodies are decoded on access.

A message is read-only, Send + Sync, and covariant in 'x, the lifetime of the raw bytes; crate::MessageParser::parse_owned and Message::into_owned give a Message<'static>. Every accessor of the root MessageRef is also available here: message.subject() is message.root().subject().

use mail_parser::{MessageParser, PartKind};

let raw = concat!(
    "From: Ann <ann@example.com>\r\n",
    "Subject: Report\r\n",
    "Content-Type: multipart/mixed; boundary=\"b\"\r\n",
    "\r\n",
    "--b\r\n",
    "Content-Type: text/plain; charset=utf-8\r\n",
    "\r\n",
    "See the attachment.\r\n",
    "--b\r\n",
    "Content-Type: application/pdf; name=report.pdf\r\n",
    "Content-Transfer-Encoding: base64\r\n",
    "\r\n",
    "JVBERi0xLjQK\r\n",
    "--b--\r\n",
);
let message = MessageParser::new().parse(raw).expect("message");
assert_eq!(message.subject(), Some("Report"));
let sender = message.from().and_then(|from| from.first());
assert_eq!(sender.and_then(|mailbox| mailbox.name()), Some("Ann"));
assert_eq!(message.body_text(0).as_deref(), Some("See the attachment."));
let attachment = message.attachments().next().expect("attachment");
assert_eq!(attachment.attachment_name(), Some("report.pdf"));
assert!(matches!(attachment.kind(), PartKind::Binary));
assert_eq!(attachment.decoded().as_ref(), b"%PDF-1.4\n");

Implementations§

Source§

impl<'x> Message<'x>

Source

pub fn raw(&self) -> &[u8] ⓘ

The raw input.

Source

pub fn root(&self) -> MessageRef<'_>

The top-level message, id 0.

Source

pub fn messages( &self, ) -> impl ExactSizeIterator<Item = MessageRef<'_>> + DoubleEndedIterator

Every message, the root first, then nested messages in document order.

Source

pub fn parts( &self, ) -> impl ExactSizeIterator<Item = MessagePart<'_>> + DoubleEndedIterator

Every part of every message, in document order.

Source

pub fn part(&self, id: PartId) -> Option<MessagePart<'_>>

The part with this id; ids number every part of every message in document order.

Source

pub fn header_count(&self) -> usize

The number of header fields of every part of every message.

Source

pub fn part_by_boundary(&self, boundary: &str) -> Option<MessagePart<'_>>

The multipart part whose boundary parameter is boundary

Source

pub fn into_owned(self) -> Message<'static>

Copies borrowed input so the message owns its bytes. Only the raw bytes are copied; the parsed store is moved.

Source

pub fn into_buffers(self) -> MessageBuffers

Drops the message and keeps its storage, cleared, for the next crate::MessageParser::parse_with; see MessageBuffers.

Source

pub fn root_part(&self) -> MessagePart<'_>

The root part of the root message; see MessageRef::root_part.

Source

pub fn headers(&self) -> Headers<'_>

The header fields of the root message.

Source

pub fn text_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>

The text body of the root message; see MessageRef::text_body.

Source

pub fn html_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>

The HTML body of the root message; see MessageRef::html_body.

Source

pub fn attachments(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>

The attachments of the root message; see MessageRef::attachments.

Source

pub fn other_parts(&self) -> impl Iterator<Item = MessagePart<'_>>

The parts of the root message in no list; see MessageRef::other_parts.

Source

pub fn has_attachments(&self) -> bool

Whether the root message has attachments; see MessageRef::has_attachments.

Source

pub fn text_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>

The text body of the root message as text; see MessageRef::text_bodies.

Source

pub fn html_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>

The HTML body of the root message as HTML; see MessageRef::html_bodies.

Source

pub fn body_text(&self, index: usize) -> Option<Cow<'_, str>>

The text of the index-th part of the root text body; see MessageRef::body_text.

Source

pub fn body_html(&self, index: usize) -> Option<Cow<'_, str>>

The HTML of the index-th part of the root HTML body; see MessageRef::body_html.

Source

pub fn body_preview(&self, max_len: usize) -> Option<Cow<'_, str>>

A preview of the root message’s body; see MessageRef::body_preview.

Source

pub fn content_type(&self) -> Option<ContentType<'_>>

The Content-Type of the root part.

Source

pub fn content_disposition(&self) -> Option<ContentType<'_>>

The Content-Disposition of the root part.

Source

pub fn attachment_name(&self) -> Option<&str>

The attachment name of the root part; see MessagePart::attachment_name.

Source

pub fn is_content_type(&self, type_: &str, subtype: &str) -> bool

Whether the Content-Type of the root part is type_/subtype (ASCII case-insensitive).

Source

pub fn subject(&self) -> Option<&str>

The Subject field, RFC 2047 encoded words decoded.

Source

pub fn comments(&self) -> Option<&str>

The Comments field, decoded.

Source

pub fn mime_version(&self) -> Option<&str>

The MIME-Version field, as written.

Source

pub fn thread_name(&self) -> Option<&str>

The subject with reply and forward markers removed; see crate::thread_name.

Source

pub fn from(&self) -> Option<AddressList<'_>>

The From field.

Source

pub fn to(&self) -> Option<AddressList<'_>>

The To field.

Source

pub fn cc(&self) -> Option<AddressList<'_>>

The Cc field.

Source

pub fn bcc(&self) -> Option<AddressList<'_>>

The Bcc field.

Source

pub fn reply_to(&self) -> Option<AddressList<'_>>

The Reply-To field.

Source

pub fn sender(&self) -> Option<AddressList<'_>>

The Sender field.

Source

pub fn all_from(&self) -> impl Iterator<Item = AddressList<'_>>

Every From field, in document order.

Source

pub fn all_to(&self) -> impl Iterator<Item = AddressList<'_>>

Every To field, in document order.

Source

pub fn all_cc(&self) -> impl Iterator<Item = AddressList<'_>>

Every Cc field, in document order.

Source

pub fn all_bcc(&self) -> impl Iterator<Item = AddressList<'_>>

Every Bcc field, in document order.

Source

pub fn resent_to(&self) -> Option<AddressList<'_>>

The Resent-To field.

Source

pub fn resent_from(&self) -> Option<AddressList<'_>>

The Resent-From field.

Source

pub fn resent_cc(&self) -> Option<AddressList<'_>>

The Resent-Cc field.

Source

pub fn resent_bcc(&self) -> Option<AddressList<'_>>

The Resent-Bcc field.

Source

pub fn resent_sender(&self) -> Option<AddressList<'_>>

The Resent-Sender field.

Source

pub fn list_archive(&self) -> Option<AddressList<'_>>

The List-Archive field; see Headers::list_archive.

Source

pub fn list_help(&self) -> Option<AddressList<'_>>

The List-Help field; see Headers::list_help.

Source

pub fn list_id(&self) -> Option<AddressList<'_>>

The List-ID field; see Headers::list_id.

Source

pub fn list_owner(&self) -> Option<AddressList<'_>>

The List-Owner field; see Headers::list_owner.

Source

pub fn list_post(&self) -> Option<AddressList<'_>>

The List-Post field; see Headers::list_post.

Source

pub fn list_subscribe(&self) -> Option<AddressList<'_>>

The List-Subscribe field; see Headers::list_subscribe.

Source

pub fn list_unsubscribe(&self) -> Option<AddressList<'_>>

The List-Unsubscribe field; see Headers::list_unsubscribe.

Source

pub fn date(&self) -> Option<DateTime>

The Date field.

Source

pub fn resent_date(&self) -> Option<DateTime>

The Resent-Date field.

Source

pub fn message_id(&self) -> Option<&str>

The last identifier of the Message-ID field, without angle brackets.

Source

pub fn in_reply_to(&self) -> Option<TextList<'_>>

The identifiers of the In-Reply-To field.

Source

pub fn references(&self) -> Option<TextList<'_>>

The identifiers of the References field.

Source

pub fn resent_message_id(&self) -> Option<TextList<'_>>

The identifiers of the Resent-Message-ID field.

Source

pub fn return_path(&self) -> Option<TextList<'_>>

The Return-Path field; see Headers::return_path.

Source

pub fn keywords(&self) -> Option<TextList<'_>>

The phrases of the Keywords field.

Source

pub fn return_address(&self) -> Option<&str>

The last address of Return-Path, or else the first address of From.

Source

pub fn received(&self) -> Option<Received<'_>>

The last Received field (the oldest hop).

Source

pub fn all_received(&self) -> impl Iterator<Item = Received<'_>>

Every Received field, in document order (newest hop first).

Trait Implementations§

Source§

impl<'x> Clone for Message<'x>

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 Message<'_>

Source§

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

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

impl Default for Message<'_>

Source§

fn default() -> Self

An empty message: no bytes, and a root part with no header and an empty body, in no body list.

Auto Trait Implementations§

§

impl<'x> Freeze for Message<'x>

§

impl<'x> RefUnwindSafe for Message<'x>

§

impl<'x> Send for Message<'x>

§

impl<'x> Sync for Message<'x>

§

impl<'x> Unpin for Message<'x>

§

impl<'x> UnsafeUnpin for Message<'x>

§

impl<'x> UnwindSafe for Message<'x>

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.