Add Message::received_all() to iterate over all Received header fields (closes #146)

This commit is contained in:
Maurus Decimus
2026-06-21 08:12:03 +02:00
parent a13fe5c32d
commit 0128bc1da0
4 changed files with 38 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
mail-parser 0.11.4
================================
- Add `Message::received_all()` to iterate over all Received header fields (#146)
- Reject dates with invalid month names.
- `parse_date()` doesn't handle UTC+12 and greater (#148)
+28
View File
@@ -0,0 +1,28 @@
use mail_parser::{MessageParser, PartType};
use std::fs;
fn main() {
let dir = "/tmp/tbtest";
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.extension().map_or(true, |e| e != "eml") { continue; }
let raw = fs::read(&path).unwrap();
let msg = MessageParser::default().parse(&raw).unwrap();
println!("\n=== {} ===", path.file_name().unwrap().to_string_lossy());
println!("text_body={:?} html_body={:?} attachments={:?}", msg.text_body, msg.html_body, msg.attachments);
for (i, p) in msg.parts.iter().enumerate() {
let kind = match &p.body {
PartType::Text(t) => format!("Text({} bytes, starts {:?})", t.len(), &t.chars().take(40).collect::<String>()),
PartType::Html(t) => format!("Html({} bytes)", t.len()),
PartType::Binary(_) => "Binary".into(),
PartType::InlineBinary(_) => "InlineBinary".into(),
PartType::Message(_) => "Message".into(),
PartType::Multipart(ids) => format!("Multipart{:?}", ids),
};
let ct = p.headers.iter().find(|h| matches!(h.name, mail_parser::HeaderName::ContentType))
.and_then(|h| h.value.as_content_type())
.map(|c| format!("{}/{}", c.ctype(), c.subtype().unwrap_or("")));
println!(" [{}] ct={:?} -> {}", i, ct, kind);
}
}
}
+8 -1
View File
@@ -248,7 +248,7 @@ impl<'x> Message<'x> {
.unwrap_or(&HeaderValue::Empty)
}
/// Returns the first Received header field
/// Returns the last Received header field
pub fn received(&self) -> Option<&Received<'x>> {
self.parts[0]
.headers
@@ -256,6 +256,13 @@ impl<'x> Message<'x> {
.and_then(|header| header.as_received())
}
/// Returns an iterator over all Received header fields, in the order they
/// appear in the message.
pub fn received_all(&self) -> impl Iterator<Item = &Received<'x>> + Sync + Send {
self.header_values(HeaderName::Received)
.filter_map(|header| header.as_received())
}
/// Returns all References header fields
pub fn references(&self) -> &HeaderValue<'x> {
self.parts[0]
+1
View File
@@ -243,6 +243,7 @@ impl MessageParser {
// Attempt to recover contents of an invalid message
let mut is_encoding_problem = offset_end == usize::MAX;
eprintln!("DBG part={} ct={:?} mime_type={:?} is_text={} is_inline={} enc_problem={} offset_end={}", state.parts, content_type.map(|c| format!("{}/{}", c.ctype(), c.subtype().unwrap_or(""))), mime_type, is_text, is_inline, is_encoding_problem, offset_end as i64);
if is_encoding_problem {
encoding = Encoding::None;
if mime_type != MimeType::TextPlain {