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>
impl<'x> Message<'x>
Sourcepub fn root(&self) -> MessageRef<'_>
pub fn root(&self) -> MessageRef<'_>
The top-level message, id 0.
Sourcepub fn messages(
&self,
) -> impl ExactSizeIterator<Item = MessageRef<'_>> + DoubleEndedIterator
pub fn messages( &self, ) -> impl ExactSizeIterator<Item = MessageRef<'_>> + DoubleEndedIterator
Every message, the root first, then nested messages in document order.
Sourcepub fn parts(
&self,
) -> impl ExactSizeIterator<Item = MessagePart<'_>> + DoubleEndedIterator
pub fn parts( &self, ) -> impl ExactSizeIterator<Item = MessagePart<'_>> + DoubleEndedIterator
Every part of every message, in document order.
Sourcepub fn part(&self, id: PartId) -> Option<MessagePart<'_>>
pub fn part(&self, id: PartId) -> Option<MessagePart<'_>>
The part with this id; ids number every part of every message in document order.
Sourcepub fn header_count(&self) -> usize
pub fn header_count(&self) -> usize
The number of header fields of every part of every message.
Sourcepub fn part_by_boundary(&self, boundary: &str) -> Option<MessagePart<'_>>
pub fn part_by_boundary(&self, boundary: &str) -> Option<MessagePart<'_>>
The multipart part whose boundary parameter is boundary
Sourcepub fn into_owned(self) -> Message<'static>
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.
Sourcepub fn into_buffers(self) -> MessageBuffers
pub fn into_buffers(self) -> MessageBuffers
Drops the message and keeps its storage, cleared, for the next
crate::MessageParser::parse_with; see MessageBuffers.
Sourcepub fn root_part(&self) -> MessagePart<'_>
pub fn root_part(&self) -> MessagePart<'_>
The root part of the root message; see MessageRef::root_part.
Sourcepub fn text_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
pub fn text_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
The text body of the root message; see MessageRef::text_body.
Sourcepub fn html_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
pub fn html_body(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
The HTML body of the root message; see MessageRef::html_body.
Sourcepub fn attachments(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
pub fn attachments(&self) -> impl ExactSizeIterator<Item = MessagePart<'_>>
The attachments of the root message; see
MessageRef::attachments.
Sourcepub fn other_parts(&self) -> impl Iterator<Item = MessagePart<'_>>
pub fn other_parts(&self) -> impl Iterator<Item = MessagePart<'_>>
The parts of the root message in no list; see
MessageRef::other_parts.
Sourcepub fn has_attachments(&self) -> bool
pub fn has_attachments(&self) -> bool
Whether the root message has attachments; see
MessageRef::has_attachments.
Sourcepub fn text_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>
pub fn text_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>
The text body of the root message as text; see
MessageRef::text_bodies.
Sourcepub fn html_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>
pub fn html_bodies(&self) -> impl Iterator<Item = Cow<'_, str>>
The HTML body of the root message as HTML; see
MessageRef::html_bodies.
Sourcepub fn body_text(&self, index: usize) -> Option<Cow<'_, str>>
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.
Sourcepub fn body_html(&self, index: usize) -> Option<Cow<'_, str>>
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.
Sourcepub fn body_preview(&self, max_len: usize) -> Option<Cow<'_, str>>
pub fn body_preview(&self, max_len: usize) -> Option<Cow<'_, str>>
A preview of the root message’s body; see
MessageRef::body_preview.
Sourcepub fn content_type(&self) -> Option<ContentType<'_>>
pub fn content_type(&self) -> Option<ContentType<'_>>
The Content-Type of the root part.
Sourcepub fn content_disposition(&self) -> Option<ContentType<'_>>
pub fn content_disposition(&self) -> Option<ContentType<'_>>
The Content-Disposition of the root part.
Sourcepub fn attachment_name(&self) -> Option<&str>
pub fn attachment_name(&self) -> Option<&str>
The attachment name of the root part; see
MessagePart::attachment_name.
Sourcepub fn is_content_type(&self, type_: &str, subtype: &str) -> bool
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).
Sourcepub fn mime_version(&self) -> Option<&str>
pub fn mime_version(&self) -> Option<&str>
The MIME-Version field, as written.
Sourcepub fn thread_name(&self) -> Option<&str>
pub fn thread_name(&self) -> Option<&str>
The subject with reply and forward markers removed; see
crate::thread_name.
Sourcepub fn from(&self) -> Option<AddressList<'_>>
pub fn from(&self) -> Option<AddressList<'_>>
The From field.
Sourcepub fn to(&self) -> Option<AddressList<'_>>
pub fn to(&self) -> Option<AddressList<'_>>
The To field.
Sourcepub fn cc(&self) -> Option<AddressList<'_>>
pub fn cc(&self) -> Option<AddressList<'_>>
The Cc field.
Sourcepub fn bcc(&self) -> Option<AddressList<'_>>
pub fn bcc(&self) -> Option<AddressList<'_>>
The Bcc field.
Sourcepub fn reply_to(&self) -> Option<AddressList<'_>>
pub fn reply_to(&self) -> Option<AddressList<'_>>
The Reply-To field.
Sourcepub fn sender(&self) -> Option<AddressList<'_>>
pub fn sender(&self) -> Option<AddressList<'_>>
The Sender field.
Sourcepub fn all_from(&self) -> impl Iterator<Item = AddressList<'_>>
pub fn all_from(&self) -> impl Iterator<Item = AddressList<'_>>
Every From field, in document order.
Sourcepub fn all_to(&self) -> impl Iterator<Item = AddressList<'_>>
pub fn all_to(&self) -> impl Iterator<Item = AddressList<'_>>
Every To field, in document order.
Sourcepub fn all_cc(&self) -> impl Iterator<Item = AddressList<'_>>
pub fn all_cc(&self) -> impl Iterator<Item = AddressList<'_>>
Every Cc field, in document order.
Sourcepub fn all_bcc(&self) -> impl Iterator<Item = AddressList<'_>>
pub fn all_bcc(&self) -> impl Iterator<Item = AddressList<'_>>
Every Bcc field, in document order.
Sourcepub fn resent_to(&self) -> Option<AddressList<'_>>
pub fn resent_to(&self) -> Option<AddressList<'_>>
The Resent-To field.
Sourcepub fn resent_from(&self) -> Option<AddressList<'_>>
pub fn resent_from(&self) -> Option<AddressList<'_>>
The Resent-From field.
Sourcepub fn resent_cc(&self) -> Option<AddressList<'_>>
pub fn resent_cc(&self) -> Option<AddressList<'_>>
The Resent-Cc field.
Sourcepub fn resent_bcc(&self) -> Option<AddressList<'_>>
pub fn resent_bcc(&self) -> Option<AddressList<'_>>
The Resent-Bcc field.
Sourcepub fn resent_sender(&self) -> Option<AddressList<'_>>
pub fn resent_sender(&self) -> Option<AddressList<'_>>
The Resent-Sender field.
Sourcepub fn list_archive(&self) -> Option<AddressList<'_>>
pub fn list_archive(&self) -> Option<AddressList<'_>>
The List-Archive field; see Headers::list_archive.
Sourcepub fn list_help(&self) -> Option<AddressList<'_>>
pub fn list_help(&self) -> Option<AddressList<'_>>
The List-Help field; see Headers::list_help.
Sourcepub fn list_id(&self) -> Option<AddressList<'_>>
pub fn list_id(&self) -> Option<AddressList<'_>>
The List-ID field; see Headers::list_id.
Sourcepub fn list_owner(&self) -> Option<AddressList<'_>>
pub fn list_owner(&self) -> Option<AddressList<'_>>
The List-Owner field; see Headers::list_owner.
Sourcepub fn list_post(&self) -> Option<AddressList<'_>>
pub fn list_post(&self) -> Option<AddressList<'_>>
The List-Post field; see Headers::list_post.
Sourcepub fn list_subscribe(&self) -> Option<AddressList<'_>>
pub fn list_subscribe(&self) -> Option<AddressList<'_>>
The List-Subscribe field; see Headers::list_subscribe.
Sourcepub fn list_unsubscribe(&self) -> Option<AddressList<'_>>
pub fn list_unsubscribe(&self) -> Option<AddressList<'_>>
The List-Unsubscribe field; see Headers::list_unsubscribe.
Sourcepub fn resent_date(&self) -> Option<DateTime>
pub fn resent_date(&self) -> Option<DateTime>
The Resent-Date field.
Sourcepub fn message_id(&self) -> Option<&str>
pub fn message_id(&self) -> Option<&str>
The last identifier of the Message-ID field, without angle brackets.
Sourcepub fn in_reply_to(&self) -> Option<TextList<'_>>
pub fn in_reply_to(&self) -> Option<TextList<'_>>
The identifiers of the In-Reply-To field.
Sourcepub fn references(&self) -> Option<TextList<'_>>
pub fn references(&self) -> Option<TextList<'_>>
The identifiers of the References field.
Sourcepub fn resent_message_id(&self) -> Option<TextList<'_>>
pub fn resent_message_id(&self) -> Option<TextList<'_>>
The identifiers of the Resent-Message-ID field.
Sourcepub fn return_path(&self) -> Option<TextList<'_>>
pub fn return_path(&self) -> Option<TextList<'_>>
The Return-Path field; see Headers::return_path.
Sourcepub fn return_address(&self) -> Option<&str>
pub fn return_address(&self) -> Option<&str>
The last address of Return-Path, or else the first address of From.
Sourcepub fn all_received(&self) -> impl Iterator<Item = Received<'_>>
pub fn all_received(&self) -> impl Iterator<Item = Received<'_>>
Every Received field, in document order (newest hop first).