msg_parser 0.3.6

Outlook Email Message (.msg) parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
use std::{io::Read, path::Path, sync::LazyLock};

use regex::Regex;

use serde::{Deserialize, Serialize};

use crate::ole;

use super::{
    error::Error,
    storage::{Properties, Storages},
};

const B64_CHARS: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

fn base64_encode(data: &[u8]) -> String {
    let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
    for chunk in data.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
        let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
        let triple = (b0 << 16) | (b1 << 8) | b2;
        out.push(B64_CHARS[((triple >> 18) & 0x3F) as usize] as char);
        out.push(B64_CHARS[((triple >> 12) & 0x3F) as usize] as char);
        if chunk.len() > 1 {
            out.push(B64_CHARS[((triple >> 6) & 0x3F) as usize] as char);
        } else {
            out.push('=');
        }
        if chunk.len() > 2 {
            out.push(B64_CHARS[(triple & 0x3F) as usize] as char);
        } else {
            out.push('=');
        }
    }
    out
}

type Name = String;
type Email = String;

static RE_CONTENT_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?im)^Content-Type: (.*(\n\s.*)*)\r\n").unwrap());
static RE_DATE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)Date: (.*(\n\s.*)*)\r\n").unwrap());
static RE_MESSAGE_ID: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?im)^Message-ID: (.*(\n\s.*)*)\r\n").unwrap());
static RE_REPLY_TO: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?im)^Reply-To: (.*(\n\s.*)*)\r\n").unwrap());

/// SMTP transport headers from the message envelope.
///
/// Contains a few commonly-used headers extracted via regex, plus the full
/// raw header text for custom parsing. Messages that were never sent via
/// SMTP (e.g. drafts) will have all fields empty.
#[non_exhaustive]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct TransportHeaders {
    /// The full, unparsed transport header text.
    pub raw: String,
    /// The `Content-Type` header value.
    pub content_type: String,
    /// The `Date` header value.
    pub date: String,
    /// The `Message-ID` header value.
    pub message_id: String,
    /// The `Reply-To` header value.
    pub reply_to: String,
}

impl TransportHeaders {
    fn extract_field(text: &str, re: &Regex) -> String {
        if text.is_empty() {
            return String::new();
        }
        re.captures(text)
            .and_then(|cap| cap.get(1).map(|x| String::from(x.as_str())))
            .unwrap_or_default()
    }

    /// Parse transport headers from raw header text.
    pub fn create_from_headers_text(text: &str) -> Self {
        Self {
            raw: text.to_string(),
            content_type: Self::extract_field(text, &RE_CONTENT_TYPE),
            date: Self::extract_field(text, &RE_DATE),
            message_id: Self::extract_field(text, &RE_MESSAGE_ID),
            reply_to: Self::extract_field(text, &RE_REPLY_TO),
        }
    }
}

/// A person referenced in the message (sender, recipient, CC, or BCC).
#[non_exhaustive]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Person {
    /// Display name (e.g. `"John Doe"`).
    pub name: Name,
    /// SMTP email address (e.g. `"john@example.com"`).
    pub email: Email,
}

impl Person {
    #[cfg(test)]
    fn new(name: Name, email: Email) -> Self {
        Self { name, email }
    }
    fn create_from_props(props: &Properties, name_key: &str, email_keys: &[&str]) -> Self {
        let name: String = props.get(name_key).map_or(String::new(), |x| x.into());
        // Get the first email that can be found in props given email_keys.
        let email = email_keys
            .iter()
            .map(|&key| props.get(key).map_or(String::new(), |x| x.into()))
            .find(|x| !x.is_empty())
            .unwrap_or_default();
        Self { name, email }
    }

    /// Returns `true` if the email address looks like an Exchange X.500 DN
    /// rather than a proper SMTP address.
    fn is_x500_dn(email: &str) -> bool {
        let upper = email.to_ascii_uppercase();
        upper.starts_with("/O=") || upper.starts_with("/CN=")
    }

    /// Try to resolve X.500 DN addresses to SMTP by searching the raw
    /// transport headers for the display name.
    fn resolve_email(&mut self, raw_headers: &str) {
        if self.email.is_empty() || !Self::is_x500_dn(&self.email) {
            return;
        }
        // Try to find this person's SMTP address in the transport headers.
        // Headers often contain: "Display Name" <user@example.com>
        // Look for the display name (or the /CN= tail) followed by an SMTP address.
        if let Some(smtp) = Self::find_smtp_in_headers(raw_headers, &self.name) {
            self.email = smtp;
        }
    }

    /// Search raw transport headers for an SMTP address associated with a display name.
    fn find_smtp_in_headers(headers: &str, display_name: &str) -> Option<String> {
        if display_name.is_empty() || headers.is_empty() {
            return None;
        }
        // Look for patterns like: "Display Name" <user@domain.com>
        // or: Display Name <user@domain.com>
        let name_lower = display_name.to_lowercase();
        for line in headers.lines() {
            let line_lower = line.to_lowercase();
            if !line_lower.contains(&name_lower) {
                continue;
            }
            // Extract email from angle brackets
            if let Some(start) = line.rfind('<')
                && let Some(end) = line[start..].find('>')
            {
                let candidate = &line[start + 1..start + end];
                if candidate.contains('@') {
                    return Some(candidate.to_string());
                }
            }
        }
        None
    }
}

/// A file attachment on the message.
///
/// # Saving an attachment to disk
///
/// ```no_run
/// # let outlook = msg_parser::Outlook::from_path("email.msg").unwrap();
/// for attach in &outlook.attachments {
///     let name = if attach.long_file_name.is_empty() {
///         &attach.file_name
///     } else {
///         &attach.long_file_name
///     };
///     std::fs::write(name, &attach.payload_bytes).unwrap();
/// }
/// ```
#[non_exhaustive]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Attachment {
    /// Display name shown in the mail client.
    pub display_name: String,
    /// Attachment content as a hex-encoded string. Use [`payload_bytes`](Attachment::payload_bytes) for raw bytes.
    pub payload: String,
    /// Attachment content as raw bytes. Identical data to `payload`, just not hex-encoded.
    #[serde(with = "hex")]
    pub payload_bytes: Vec<u8>,
    /// File extension including the dot (e.g. `".pdf"`).
    pub extension: String,
    /// MIME type (e.g. `"image/png"`). May be empty.
    pub mime_tag: String,
    /// Short 8.3 filename (e.g. `"docume~1.pdf"`).
    pub file_name: String,
    /// Full original filename (e.g. `"document_final.pdf"`).
    pub long_file_name: String,
    /// How the attachment is stored. Common values:
    /// - `1` — by value (regular file, bytes in `payload_bytes`)
    /// - `5` — embedded message (nested `.msg`)
    /// - `6` — OLE object
    pub attach_method: u32,
    /// Content-ID for inline attachments (e.g. `"image001@01D00000.00000000"`).
    /// Used to resolve `cid:` references in HTML bodies. Empty if not set.
    pub content_id: String,
}

impl Attachment {
    fn create(storages: &Storages, idx: usize) -> Self {
        let payload_bytes = storages.get_bytes_from_attachment(idx, "AttachDataObject");
        let payload = hex::encode(&payload_bytes);
        Self {
            display_name: storages.get_val_from_attachment_or_default(idx, "DisplayName"),
            payload,
            payload_bytes,
            extension: storages.get_val_from_attachment_or_default(idx, "AttachExtension"),
            mime_tag: storages.get_val_from_attachment_or_default(idx, "AttachMimeTag"),
            file_name: storages.get_val_from_attachment_or_default(idx, "AttachFilename"),
            long_file_name: storages.get_val_from_attachment_or_default(idx, "AttachLongFilename"),
            attach_method: storages
                .get_attachment_int_prop(idx, "AttachMethod")
                .unwrap_or(0),
            content_id: storages.get_val_from_attachment_or_default(idx, "AttachContentId"),
        }
    }

    /// Returns `true` if this attachment is an embedded `.msg` message
    /// (`attach_method == 5`).
    pub fn is_embedded_message(&self) -> bool {
        self.attach_method == 5
    }

    /// Parse the embedded `.msg` attachment and return the nested message.
    ///
    /// Returns `Some(Ok(outlook))` if this is an embedded message (`attach_method == 5`)
    /// with parseable content, `Some(Err(_))` if it is an embedded message but parsing
    /// fails, or `None` if it is not an embedded message.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// for attach in &outlook.attachments {
    ///     if let Some(Ok(nested)) = attach.as_message() {
    ///         println!("Embedded: {}", nested.subject);
    ///     }
    /// }
    /// ```
    pub fn as_message(&self) -> Option<Result<Outlook, Error>> {
        if !self.is_embedded_message() || self.payload_bytes.is_empty() {
            return None;
        }
        Some(Outlook::from_slice(&self.payload_bytes))
    }
}

/// A parsed Outlook `.msg` email message.
///
/// Create an instance with [`from_path`](Outlook::from_path),
/// [`from_slice`](Outlook::from_slice), or [`from_reader`](Outlook::from_reader),
/// then access the message fields directly.
///
/// Field names follow the
/// [MS-OXPROPS](https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxprops/f6ab1613-aefe-447d-a49c-18217230b148)
/// specification.
///
/// # Example
///
/// ```no_run
/// use msg_parser::Outlook;
///
/// let outlook = Outlook::from_path("email.msg").unwrap();
/// println!("Subject: {}", outlook.subject);
/// println!("From: {} <{}>", outlook.sender.name, outlook.sender.email);
/// println!("To: {:?}", outlook.to);
/// println!("CC: {:?}", outlook.cc);
/// println!("BCC: {:?}", outlook.bcc);
/// println!("Delivered: {}", outlook.message_delivery_time);
/// println!("Attachments: {}", outlook.attachments.len());
/// ```
#[non_exhaustive]
#[derive(Serialize, Deserialize, Debug)]
pub struct Outlook {
    /// SMTP transport headers. Empty for messages that were never sent.
    pub headers: TransportHeaders,
    /// Message sender.
    pub sender: Person,
    /// Primary recipients (`RecipientType = 1`).
    pub to: Vec<Person>,
    /// Carbon-copy recipients (`RecipientType = 2`).
    pub cc: Vec<Person>,
    /// Blind carbon-copy recipients (`RecipientType = 3`).
    pub bcc: Vec<Person>,
    /// Message subject line.
    pub subject: String,
    /// Plain-text body.
    pub body: String,
    /// HTML body. Empty if the message has no HTML representation.
    pub html: String,
    /// RTF compressed body (hex-encoded).
    pub rtf_compressed: String,
    /// Message class, typically `"IPM.Note"` for regular emails.
    pub message_class: String,
    /// Importance level: `0` = Low, `1` = Normal, `2` = High.
    pub importance: u32,
    /// Sensitivity level: `0` = Normal, `1` = Personal, `2` = Private, `3` = Confidential.
    pub sensitivity: u32,
    /// When the sender submitted the message (ISO 8601 UTC). Empty if unavailable.
    pub client_submit_time: String,
    /// When the message was delivered (ISO 8601 UTC). Empty if unavailable.
    pub message_delivery_time: String,
    /// When the message object was created (ISO 8601 UTC). Empty if unavailable.
    pub creation_time: String,
    /// When the message was last modified (ISO 8601 UTC). Empty if unavailable.
    pub last_modification_time: String,
    /// File attachments. See [`Attachment`] for details.
    pub attachments: Vec<Attachment>,
    /// Named properties from the MAPI 0x8000+ range (e.g. `ReminderSet`,
    /// `InternetAccountName`, or custom string-named properties).
    /// Keys are the resolved property names, values are the string
    /// representation of the property value.
    pub named_properties: std::collections::HashMap<String, String>,
}

impl Outlook {
    fn populate(storages: &Storages) -> Self {
        let headers_text = storages.get_val_from_root_or_default("TransportMessageHeaders");
        let headers = TransportHeaders::create_from_headers_text(&headers_text);

        let mut to = Vec::new();
        let mut cc = Vec::new();
        let mut bcc = Vec::new();

        for (i, recip_map) in storages.recipients.iter().enumerate() {
            let mut person = Person::create_from_props(
                recip_map,
                "DisplayName",
                &["SmtpAddress", "EmailAddress"],
            );
            // Resolve X.500 DN addresses to SMTP via transport headers
            person.resolve_email(&headers_text);
            // RecipientType: 1=To, 2=CC, 3=BCC (MS-OXMSG 2.2.1)
            match storages.get_recipient_int_prop(i, "RecipientType") {
                Some(2) => cc.push(person),
                Some(3) => bcc.push(person),
                _ => to.push(person), // Default to To (including type==1 and missing)
            }
        }

        let mut sender = Person::create_from_props(
            &storages.root,
            "SenderName",
            &["SenderSmtpAddress", "SenderEmailAddress"],
        );
        sender.resolve_email(&headers_text);

        Self {
            headers,
            sender,
            to,
            cc,
            bcc,
            subject: storages.get_val_from_root_or_default("Subject"),
            body: storages.get_val_from_root_or_default("Body"),
            html: storages.get_val_from_root_or_default("Html"),
            rtf_compressed: storages.get_val_from_root_or_default("RtfCompressed"),
            message_class: storages.get_val_from_root_or_default("MessageClass"),
            importance: storages.get_root_int_prop("Importance").unwrap_or(1),
            sensitivity: storages.get_root_int_prop("Sensitivity").unwrap_or(0),
            client_submit_time: storages.get_val_from_root_or_default("ClientSubmitTime"),
            message_delivery_time: storages.get_val_from_root_or_default("MessageDeliveryTime"),
            creation_time: storages.get_val_from_root_or_default("CreationTime"),
            last_modification_time: storages.get_val_from_root_or_default("LastModificationTime"),
            attachments: storages
                .attachments
                .iter()
                .enumerate()
                .map(|(i, _)| Attachment::create(storages, i))
                .collect(),
            named_properties: {
                let named_names = storages.named_property_names();
                storages
                    .root
                    .iter()
                    .filter(|(k, _)| named_names.contains(k.as_str()))
                    .map(|(k, v)| (k.clone(), String::from(v)))
                    .collect()
            },
        }
    }

    /// Parse a `.msg` file from a filesystem path.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// println!("{}", outlook.subject);
    /// ```
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        let data = std::fs::read(path)?;
        let parser = ole::Reader::from_bytes(data)?;
        let mut storages = Storages::new(&parser);
        storages.process_streams(&parser);

        let outlook = Self::populate(&storages);
        Ok(outlook)
    }

    /// Parse a `.msg` file from any [`Read`](std::io::Read) source.
    ///
    /// Reads the entire stream into memory, then parses. Useful for stdin,
    /// network streams, or any non-seekable source.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let file = std::fs::File::open("email.msg").unwrap();
    /// let outlook = Outlook::from_reader(file).unwrap();
    /// ```
    pub fn from_reader<R: std::io::Read>(reader: R) -> Result<Self, Error> {
        // Cap reads at 256 MB to prevent unbounded memory allocation
        const MAX_SIZE: u64 = 256 * 1024 * 1024;
        let mut limited = reader.take(MAX_SIZE + 1);
        let mut buf = Vec::new();
        limited.read_to_end(&mut buf)?;
        if buf.len() as u64 > MAX_SIZE {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Input exceeds maximum allowed size (256 MB)",
            )
            .into());
        }
        // Use from_bytes directly to avoid an extra copy through from_slice
        let parser = ole::Reader::from_bytes(buf)?;
        let mut storages = Storages::new(&parser);
        storages.process_streams(&parser);
        Ok(Self::populate(&storages))
    }

    /// Parse a `.msg` file from a byte slice already in memory.
    ///
    /// Accepts any type that implements `AsRef<[u8]>`, including `&[u8]`,
    /// `Vec<u8>`, and `bytes::Bytes`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let bytes = std::fs::read("email.msg").unwrap();
    /// let outlook = Outlook::from_slice(&bytes).unwrap();
    /// ```
    pub fn from_slice(slice: impl AsRef<[u8]>) -> Result<Self, Error> {
        let parser = ole::Reader::from_bytes(slice.as_ref())?;
        let mut storages = Storages::new(&parser);
        storages.process_streams(&parser);

        let outlook = Self::populate(&storages);
        Ok(outlook)
    }

    /// Serialize the parsed message to a JSON string.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// let json = outlook.to_json().unwrap();
    /// println!("{}", json);
    /// ```
    pub fn to_json(&self) -> Result<String, Error> {
        Ok(serde_json::to_string(self)?)
    }

    /// Decompress the RTF body and return it as a byte vector.
    ///
    /// The `rtf_compressed` field contains the raw compressed data as a
    /// hex-encoded string. This method decodes and decompresses it per
    /// [MS-OXRTFCP](https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfcp).
    ///
    /// Returns `None` if the message has no RTF body or decompression fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// if let Some(rtf) = outlook.rtf_decompressed() {
    ///     println!("RTF body: {} bytes", rtf.len());
    /// }
    /// ```
    pub fn rtf_decompressed(&self) -> Option<Vec<u8>> {
        if self.rtf_compressed.is_empty() {
            return None;
        }
        let raw = hex::decode(&self.rtf_compressed).ok()?;
        super::rtf::decompress_rtf(&raw)
    }

    /// Extract HTML from the RTF body when the message has no direct HTML property.
    ///
    /// Many Outlook messages embed the HTML body inside compressed RTF using
    /// the `\fromhtml1` control word. This method decompresses the RTF and
    /// extracts the embedded HTML.
    ///
    /// Returns `None` if the RTF body doesn't contain embedded HTML.
    /// Prefer the `html` field when it is non-empty — this method is a fallback.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// let html = if !outlook.html.is_empty() {
    ///     outlook.html.clone()
    /// } else {
    ///     outlook.html_from_rtf().unwrap_or_default()
    /// };
    /// ```
    pub fn html_from_rtf(&self) -> Option<String> {
        let rtf = self.rtf_decompressed()?;
        super::rtf::extract_html_from_rtf(&rtf)
    }

    /// Find an attachment by its Content-ID.
    ///
    /// HTML bodies reference inline images via `cid:` URIs. This method
    /// resolves a Content-ID string to the matching attachment.
    ///
    /// The `cid` parameter should be the bare identifier (without the `cid:`
    /// scheme prefix).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// if let Some(img) = outlook.attachment_by_content_id("image001@01D00000.00000000") {
    ///     println!("Found inline image: {} ({} bytes)", img.long_file_name, img.payload_bytes.len());
    /// }
    /// ```
    pub fn attachment_by_content_id(&self, cid: &str) -> Option<&Attachment> {
        if cid.is_empty() {
            return None;
        }
        self.attachments
            .iter()
            .find(|a| !a.content_id.is_empty() && a.content_id == cid)
    }

    /// Resolve all `cid:` references in an HTML string, replacing them with
    /// inline `data:` URIs using base64-encoded attachment content.
    ///
    /// Returns the HTML with all resolvable `cid:` references replaced.
    /// Unresolvable references are left as-is.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use msg_parser::Outlook;
    ///
    /// let outlook = Outlook::from_path("email.msg").unwrap();
    /// let html = outlook.resolve_cid_references(&outlook.html);
    /// // All cid:image001@... references are now data: URIs
    /// std::fs::write("email.html", html).unwrap();
    /// ```
    pub fn resolve_cid_references(&self, html: &str) -> String {
        let mut result = html.to_string();
        for attach in &self.attachments {
            if attach.content_id.is_empty() {
                continue;
            }
            let cid_ref = format!("cid:{}", attach.content_id);
            if !result.contains(&cid_ref) {
                continue;
            }
            let mime = if attach.mime_tag.is_empty() {
                "application/octet-stream"
            } else {
                &attach.mime_tag
            };
            let b64 = base64_encode(&attach.payload_bytes);
            let data_uri = format!("data:{};base64,{}", mime, b64);
            result = result.replace(&cid_ref, &data_uri);
        }
        result
    }
}

impl std::fmt::Display for Person {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.name.is_empty() {
            write!(f, "{}", self.email)
        } else if self.email.is_empty() || self.name == self.email {
            write!(f, "{}", self.name)
        } else {
            write!(f, "{} <{}>", self.name, self.email)
        }
    }
}

impl std::fmt::Display for Attachment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = if self.long_file_name.is_empty() {
            if self.file_name.is_empty() {
                &self.display_name
            } else {
                &self.file_name
            }
        } else {
            &self.long_file_name
        };
        let method = match self.attach_method {
            1 => "file",
            5 => "embedded .msg",
            6 => "OLE object",
            _ => "unknown",
        };
        write!(
            f,
            "{} ({}, {} bytes)",
            name,
            method,
            self.payload_bytes.len()
        )
    }
}

impl std::fmt::Display for Outlook {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "From:    {}", self.sender)?;
        writeln!(f, "Subject: {}", self.subject)?;
        if !self.to.is_empty() {
            let to: Vec<String> = self.to.iter().map(|p| p.to_string()).collect();
            writeln!(f, "To:      {}", to.join(", "))?;
        }
        if !self.cc.is_empty() {
            let cc: Vec<String> = self.cc.iter().map(|p| p.to_string()).collect();
            writeln!(f, "CC:      {}", cc.join(", "))?;
        }
        if !self.bcc.is_empty() {
            let bcc: Vec<String> = self.bcc.iter().map(|p| p.to_string()).collect();
            writeln!(f, "BCC:     {}", bcc.join(", "))?;
        }
        if !self.message_delivery_time.is_empty() {
            writeln!(f, "Date:    {}", self.message_delivery_time)?;
        }
        if !self.attachments.is_empty() {
            writeln!(f, "Attachments ({}):", self.attachments.len())?;
            for a in &self.attachments {
                writeln!(f, "  - {}", a)?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{Outlook, Person, TransportHeaders};

    #[test]
    fn test_invalid_file() {
        let path = "data/bad_outlook.msg";
        let err = Outlook::from_path(path).unwrap_err();
        assert_eq!(
            err.to_string(),
            "Error parsing file with ole: Invalid OLE File".to_string()
        );
    }

    #[test]
    fn test_transport_header_test_email_1() {
        use super::super::storage::Storages;
        use crate::ole::Reader;

        let parser = Reader::from_path("data/test_email.msg").unwrap();
        let mut storages = Storages::new(&parser);
        storages.process_streams(&parser);

        let transport_text = storages.get_val_from_root_or_default("TransportMessageHeaders");

        let header = TransportHeaders::create_from_headers_text(&transport_text);

        assert!(header.raw.is_empty());
        assert!(header.content_type.is_empty());
        assert!(header.date.is_empty());
        assert!(header.message_id.is_empty());
        assert!(header.reply_to.is_empty());
    }

    #[test]
    fn test_test_email() {
        let path = "data/test_email.msg";
        let outlook = Outlook::from_path(path).unwrap();
        assert_eq!(
            outlook.sender,
            Person {
                name: "".to_string(),
                email: "".to_string()
            }
        );

        // RecipientType == 1 (To)
        assert_eq!(
            outlook.to,
            vec![Person {
                name: "marirs@outlook.com".to_string(),
                email: "marirs@outlook.com".to_string()
            }]
        );

        // RecipientType == 2 (CC)
        assert_eq!(
            outlook.cc,
            vec![
                Person {
                    name: "Sriram Govindan".to_string(),
                    email: "marirs@aol.in".to_string()
                },
                Person {
                    name: "marirs@outlook.in".to_string(),
                    email: "marirs@outlook.in".to_string()
                },
            ]
        );

        // RecipientType == 3 (BCC)
        assert_eq!(
            outlook.bcc,
            vec![
                Person {
                    name: "Sriram Govindan".to_string(),
                    email: "marirs@aol.in".to_string()
                },
                Person {
                    name: "Sriram Govindan".to_string(),
                    email: "marirs@outlook.com".to_string()
                },
                Person {
                    name: "marirs@outlook.in".to_string(),
                    email: "marirs@outlook.in".to_string()
                },
            ]
        );

        assert_eq!(outlook.subject, String::from("Test Email"));

        assert!(outlook.headers.raw.is_empty());
        assert!(outlook.headers.content_type.is_empty());

        assert!(outlook.body.starts_with("Test Email\r\n"));
        assert!(outlook.rtf_compressed.starts_with("51210000c8a200004c5a4"));
    }

    #[test]
    fn test_test_email_2() {
        let path = "data/test_email.msg";
        let outlook = Outlook::from_path(path).unwrap();
        assert_eq!(
            outlook.sender,
            Person {
                name: "".to_string(),
                email: "".to_string()
            }
        );
        assert_eq!(outlook.to.len(), 1);
        assert_eq!(outlook.cc.len(), 2);
        assert_eq!(outlook.bcc.len(), 3);
        assert_eq!(outlook.subject, String::from("Test Email"));

        assert!(outlook.body.starts_with("Test Email"));

        assert_eq!(outlook.attachments.len(), 3);
        // Check displaynames
        let displays: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.display_name.clone())
            .collect();
        assert_eq!(
            displays,
            vec![
                "1 Days Left—35% off cloud space, upgrade now!".to_string(),
                "milky-way-2695569_960_720.jpg".to_string(),
                "Test Email.msg".to_string(),
            ]
        );
        // Check extensions
        let exts: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.extension.clone())
            .collect();
        assert_eq!(
            exts,
            vec!["".to_string(), ".jpg".to_string(), ".msg".to_string()]
        );
        // Check mime tag
        let mimes: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.mime_tag.clone())
            .collect();
        assert_eq!(mimes, vec!["".to_string(), "".to_string(), "".to_string()]);
        // Check filenames
        let filenames: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.file_name.clone())
            .collect();
        assert_eq!(
            filenames,
            vec![
                "".to_string(),
                "milky-~1.jpg".to_string(),
                "TestEm~1.msg".to_string()
            ]
        );
    }

    #[test]
    fn test_attachment_msg() {
        let path = "data/attachment.msg";
        let outlook = Outlook::from_path(path).unwrap();
        assert_eq!(outlook.attachments.len(), 3);

        // Check displaynames
        let displays: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.display_name.clone())
            .collect();
        assert_eq!(
            displays,
            vec![
                "loan_proposal.doc".to_string(),
                "image001.png".to_string(),
                "image002.jpg".to_string()
            ]
        );
        // Check extensions
        let exts: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.extension.clone())
            .collect();
        assert_eq!(
            exts,
            vec![".doc".to_string(), ".png".to_string(), ".jpg".to_string()]
        );
        // Check mime tag
        let mimes: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.mime_tag.clone())
            .collect();
        assert_eq!(
            mimes,
            vec![
                "application/msword".to_string(),
                "image/png".to_string(),
                "image/jpeg".to_string()
            ]
        );
        // Check filenames
        let filenames: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.file_name.clone())
            .collect();
        assert_eq!(
            filenames,
            vec![
                "loan_p~1.doc".to_string(),
                "image001.png".to_string(),
                "image002.jpg".to_string()
            ]
        );
        // Check long filenames
        let long_names: Vec<String> = outlook
            .attachments
            .iter()
            .map(|x| x.long_file_name.clone())
            .collect();
        assert_eq!(
            long_names,
            vec![
                "loan_proposal.doc".to_string(),
                "image001.png".to_string(),
                "image002.jpg".to_string()
            ]
        );
    }

    #[test]
    fn test_payload_bytes() {
        let outlook = Outlook::from_path("data/attachment.msg").unwrap();

        // payload_bytes contains raw binary, payload contains hex encoding
        for attach in &outlook.attachments {
            assert_eq!(attach.payload, hex::encode(&attach.payload_bytes));
        }

        // Verify magic bytes: DOC (OLE), PNG, JPEG
        assert_eq!(
            &outlook.attachments[0].payload_bytes[..4],
            b"\xd0\xcf\x11\xe0"
        );
        assert_eq!(&outlook.attachments[1].payload_bytes[..4], b"\x89PNG");
        assert_eq!(&outlook.attachments[2].payload_bytes[..2], b"\xff\xd8");

        // Non-empty
        assert!(!outlook.attachments[0].payload_bytes.is_empty());
        assert!(!outlook.attachments[1].payload_bytes.is_empty());
        assert!(!outlook.attachments[2].payload_bytes.is_empty());
    }

    #[test]
    fn test_attach_method() {
        // test_email.msg: attachment #0 is embedded msg (method=5), #1 and #2 are by_value (method=1)
        let outlook = Outlook::from_path("data/test_email.msg").unwrap();
        assert_eq!(outlook.attachments[0].attach_method, 5); // embedded_msg
        assert_eq!(outlook.attachments[1].attach_method, 1); // by_value
        assert_eq!(outlook.attachments[2].attach_method, 1); // by_value

        // attachment.msg: all by_value
        let outlook = Outlook::from_path("data/attachment.msg").unwrap();
        for attach in &outlook.attachments {
            assert_eq!(attach.attach_method, 1);
        }
    }

    #[test]
    fn test_unicode_msg() {
        let path = "data/unicode.msg";
        let outlook = Outlook::from_path(path).unwrap();
        assert_eq!(
            outlook.sender,
            Person {
                name: "Brian Zhou".to_string(),
                email: "brizhou@gmail.com".to_string()
            }
        );
        // Recipient #0 is To
        assert_eq!(
            outlook.to,
            vec![Person {
                name: "brianzhou@me.com".to_string(),
                email: "brianzhou@me.com".to_string()
            }]
        );

        // Recipient #1 is CC
        assert_eq!(
            outlook.cc,
            vec![Person::new(
                "Brian Zhou".to_string(),
                "brizhou@gmail.com".to_string()
            )]
        );

        assert!(outlook.bcc.is_empty());
        assert_eq!(outlook.subject, String::from("Test for TIF files"));
        assert!(!outlook.headers.raw.is_empty());
        assert_eq!(
            outlook.headers.content_type,
            "multipart/mixed; boundary=001a113392ecbd7a5404eb6f4d6a"
        );
        assert_eq!(outlook.headers.date, "Mon, 18 Nov 2013 10:26:24 +0200");
        assert_eq!(
            outlook.headers.message_id,
            "<CADtJ4eNjQSkGcBtVteCiTF+YFG89+AcHxK3QZ=-Mt48xygkvdQ@mail.gmail.com>"
        );
        assert!(outlook.headers.reply_to.is_empty());
        assert!(outlook.rtf_compressed.starts_with("bc020000b908"));
    }

    #[test]
    fn test_ascii() {
        let path = "data/ascii.msg";
        let outlook = Outlook::from_path(path).unwrap();
        assert_eq!(
            outlook.sender,
            Person {
                name: "from@domain.com".to_string(),
                email: "from@domain.com".to_string()
            }
        );
        assert_eq!(
            outlook.to,
            vec![Person {
                name: "to@domain.com".to_string(),
                email: "to@domain.com".to_string()
            },]
        );

        assert_eq!(
            outlook.subject,
            String::from("creating an outlook message file")
        );
    }

    #[test]
    fn test_recipient_types() {
        let path = "data/test_email.msg";
        let outlook = Outlook::from_path(path).unwrap();

        // Verify recipient splitting by RecipientType
        assert_eq!(outlook.to.len(), 1);
        assert_eq!(outlook.cc.len(), 2);
        assert_eq!(outlook.bcc.len(), 3);

        // ascii.msg has only To recipients
        let outlook = Outlook::from_path("data/ascii.msg").unwrap();
        assert_eq!(outlook.to.len(), 1);
        assert!(outlook.cc.is_empty());
        assert!(outlook.bcc.is_empty());
    }

    #[test]
    fn test_date_fields() {
        // unicode.msg has all four date fields
        let outlook = Outlook::from_path("data/unicode.msg").unwrap();
        assert_eq!(outlook.client_submit_time, "2013-11-18T08:26:24Z");
        assert_eq!(outlook.message_delivery_time, "2013-11-18T08:26:29Z");
        assert!(outlook.creation_time.starts_with("2013-11-18T08:32:28"));
        assert!(
            outlook
                .last_modification_time
                .starts_with("2013-11-18T08:32:28")
        );

        // test_email.msg has delivery time but no submit time
        let outlook = Outlook::from_path("data/test_email.msg").unwrap();
        assert!(outlook.client_submit_time.is_empty());
        assert!(
            outlook
                .message_delivery_time
                .starts_with("2021-01-05T03:00:32")
        );
        assert!(outlook.creation_time.starts_with("2021-01-05T03:13:18"));

        // ascii.msg has no submit/delivery times
        let outlook = Outlook::from_path("data/ascii.msg").unwrap();
        assert!(outlook.client_submit_time.is_empty());
        assert!(outlook.message_delivery_time.is_empty());
        assert!(outlook.creation_time.starts_with("2017-06-01T15:24:31"));
    }

    #[test]
    fn test_message_class_importance_sensitivity() {
        let outlook = Outlook::from_path("data/test_email.msg").unwrap();
        assert_eq!(outlook.message_class, "IPM.Note");
        assert_eq!(outlook.importance, 1); // Normal
        assert_eq!(outlook.sensitivity, 0); // Normal

        let outlook = Outlook::from_path("data/unicode.msg").unwrap();
        assert_eq!(outlook.message_class, "IPM.Note");
        assert_eq!(outlook.importance, 1);
        // unicode.msg has no sensitivity property, defaults to 0
        assert_eq!(outlook.sensitivity, 0);

        let outlook = Outlook::from_path("data/ascii.msg").unwrap();
        assert_eq!(outlook.message_class, "IPM.Note");
    }

    #[test]
    fn test_from_reader() {
        let file = std::fs::File::open("data/unicode.msg").unwrap();
        let reader_outlook = Outlook::from_reader(file).unwrap();
        let path_outlook = Outlook::from_path("data/unicode.msg").unwrap();

        assert_eq!(reader_outlook.subject, path_outlook.subject);
        assert_eq!(reader_outlook.sender, path_outlook.sender);
        assert_eq!(reader_outlook.to, path_outlook.to);
        assert_eq!(reader_outlook.cc, path_outlook.cc);
    }

    #[test]
    fn test_to_json() {
        let path = "data/test_email.msg";
        let outlook = Outlook::from_path(path).unwrap();
        let json = outlook.to_json().unwrap();
        assert!(!json.is_empty());
    }

    #[test]
    fn test_html_field_present() {
        // test_email.msg has no Html property, so html should be empty
        let outlook = Outlook::from_path("data/test_email.msg").unwrap();
        assert!(outlook.html.is_empty());

        // unicode.msg also has no Html property
        let outlook = Outlook::from_path("data/unicode.msg").unwrap();
        assert!(outlook.html.is_empty());
    }

    #[test]
    fn test_html_in_json_output() {
        let outlook = Outlook::from_path("data/test_email.msg").unwrap();
        let json = outlook.to_json().unwrap();
        assert!(json.contains("\"html\""));
    }
}