Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): cut Outlook replies at the horizontal rule (#8823)

Jacob Beckermanc099ac766f4c
diff --git a/crates/email_utils/AGENTS.md b/crates/email_utils/AGENTS.md
index 544ee8118d..477d90c264 100644
--- a/crates/email_utils/AGENTS.md
+++ b/crates/email_utils/AGENTS.md
@@ -43,6 +43,9 @@ sender/generic-address checks.
- Enable `scraper`'s `deterministic` feature: thread reads recompute replyless
HTML, and shuffled attributes rebuild otherwise unchanged email bodies.
+- Outlook mobile and Outlook on the web start the quoted thread at `<hr>` plus
+ `div#divRplyFwdMsg` (plaintext: a line of underscores, then `From:`). Cut
+ there. A later `border-top` "From:" block is a nested quote.
- Sanitization is security-sensitive (XSS via email HTML): any allowlist
change needs a test proving the previously-dangerous input is now stripped,
not just that benign input survives.
diff --git a/crates/email_utils/src/body_replyless/constants.rs b/crates/email_utils/src/body_replyless/constants.rs
index d9a2aec7ff..c829e5a995 100644
--- a/crates/email_utils/src/body_replyless/constants.rs
+++ b/crates/email_utils/src/body_replyless/constants.rs
@@ -49,6 +49,11 @@ lazy_static! {
^--+\s*original\s+message\s*--+
)
| # OR
+ ( # Outlook's reply rule: a line of underscores, then a From: header.
+ # The cut is the start of this match, so the rule itself is dropped.
+ ^_{10,}[^\S\n]*\n\s*From:
+ )
+ | # OR
( # Group 3: Forwarded headers like > From: or From:
^>?>?\s*From:
)
@@ -69,6 +74,10 @@ lazy_static! {
pub static ref OUTLOOK_DIV_SELECTOR: Selector = Selector::parse("div").unwrap();
pub static ref OUTLOOK_REPLY_CONTAINER_SELECTOR: Selector = Selector::parse("div#mail-editor-reference-message-container").unwrap();
pub static ref OUTLOOK_APPENDONSEND_SELECTOR: Selector = Selector::parse("div#appendonsend").unwrap();
+ /// Outlook on the web and Outlook mobile put the quoted thread in this div.
+ /// Nested copies are prefixed (`x_divRplyFwdMsg`); the unsuffixed id is the
+ /// start of the quote on the message being read.
+ pub static ref OUTLOOK_REPLY_HEADER_SELECTOR: Selector = Selector::parse("div#divRplyFwdMsg").unwrap();
pub static ref BLOCKQUOTE_SELECTOR: Selector = Selector::parse("blockquote").unwrap();
pub static ref ANY_ELEMENT_SELECTOR: Selector = Selector::parse("*").unwrap();
pub static ref BODY_SELECTOR: Selector = Selector::parse("body").unwrap();
diff --git a/crates/email_utils/src/body_replyless/html_parser.rs b/crates/email_utils/src/body_replyless/html_parser.rs
index 999627b179..ac9eb716b4 100644
--- a/crates/email_utils/src/body_replyless/html_parser.rs
+++ b/crates/email_utils/src/body_replyless/html_parser.rs
@@ -88,6 +88,12 @@ fn find_outlook_splitter(document: &Html) -> Option<ElementRef<'_>> {
if let Some(container) = document.select(&OUTLOOK_REPLY_CONTAINER_SELECTOR).next() {
return Some(container);
}
+ // Outlook mobile and Outlook on the web put a horizontal rule immediately
+ // above `div#divRplyFwdMsg`. A later `border-top` "From:" block is usually a
+ // quote nested inside that thread, so the rule has to win.
+ if let Some(header) = document.select(&OUTLOOK_REPLY_HEADER_SELECTOR).next() {
+ return Some(outlook_reply_header_splitter(header));
+ }
// Check for a bordered element containing the header text.
for element in document.select(&ANY_ELEMENT_SELECTOR) {
@@ -138,6 +144,57 @@ fn find_text_based_splitter(document: &Html) -> Option<ElementRef<'_>> {
None
}
+/// The quoted thread starts at the horizontal rule when one sits directly
+/// above the reply header. Empty wrappers around that rule count; real reply
+/// text in between means the header itself is the cut.
+fn outlook_reply_header_splitter(header: ElementRef<'_>) -> ElementRef<'_> {
+ let mut sibling = header.prev_sibling();
+ while let Some(node) = sibling {
+ if let Some(element) = ElementRef::wrap(node) {
+ if is_horizontal_rule(element) {
+ return element;
+ }
+ if element.text().collect::<String>().trim().is_empty() {
+ sibling = node.prev_sibling();
+ continue;
+ }
+ return header;
+ }
+ if let Some(text) = node.value().as_text()
+ && !text.trim().is_empty()
+ {
+ return header;
+ }
+ sibling = node.prev_sibling();
+ }
+ header
+}
+
+/// An `hr`, or an element that contains nothing else (Outlook centers the rule
+/// in a div).
+fn is_horizontal_rule(element: ElementRef<'_>) -> bool {
+ if element.value().name().eq_ignore_ascii_case("hr") {
+ return true;
+ }
+ let mut saw_rule = false;
+ for child in element.children() {
+ if let Some(child_element) = ElementRef::wrap(child) {
+ if child_element.value().name().eq_ignore_ascii_case("br") {
+ continue;
+ }
+ if !is_horizontal_rule(child_element) {
+ return false;
+ }
+ saw_rule = true;
+ } else if let Some(text) = child.value().as_text()
+ && !text.trim().is_empty()
+ {
+ return false;
+ }
+ }
+ saw_rule
+}
+
// --- Low-Level Utilities ---
/// Finds the previous non-trivial sibling element of a given element, skipping
diff --git a/crates/email_utils/src/body_replyless/test.rs b/crates/email_utils/src/body_replyless/test.rs
index 9b41d372df..03ccc986c9 100644
--- a/crates/email_utils/src/body_replyless/test.rs
+++ b/crates/email_utils/src/body_replyless/test.rs
@@ -62,6 +62,122 @@ fn test_extract_message_reply_test_4() {
test_email_extraction(full_email, expected_reply, "test-4");
}
+/// Outlook mobile (and Outlook on the web) separate the quote with a plain
+/// `<hr>` plus `div#divRplyFwdMsg`. A `border-top` header further down is a
+/// nested quote and must not be the cut.
+#[test]
+fn outlook_mobile_reply_cuts_at_the_horizontal_rule() {
+ let full_email = r#"
+<div>Hi Madalyn – hope you had a great weekend!</div>
+<div id="ms-outlook-mobile-body-separator-line"><div><br></div></div>
+<div id="ms-outlook-mobile-signature">
+<div>Eric Siegel</div>
+<div>732.615.8506</div>
+</div>
+<hr style="display:inline-block;width:98%" tabindex="-1">
+<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif"><b>From:</b> Eric Siegel &lt;[email protected]&gt;<br>
+<b>Sent:</b> Thursday, 22 October 2026 11:57:19<br>
+<b>To:</b> Madalyn Gibbons &lt;[email protected]&gt;<br>
+<b>Subject:</b> RE: Renewal</font>
+<div>&nbsp;</div>
+</div>
+<div>
+<p>Quoted body that must be removed.</p>
+<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
+<p><b>From:</b> Older Sender<br><b>Sent:</b> Monday<br><b>To:</b> Eric<br><b>Subject:</b> Older</p>
+</div>
+<p>Even older quoted body.</p>
+</div>
+"#;
+ let sanitized = crate::sanitize_email_html(full_email);
+ let reply =
+ compute_body_replyless(Some("Re: Renewal"), Some(&sanitized), None).expect("html body");
+
+ assert!(
+ reply.contains("hope you had a great weekend"),
+ "reply was dropped: {reply}"
+ );
+ assert!(
+ reply.contains("732.615.8506"),
+ "signature was dropped: {reply}"
+ );
+ assert!(
+ !reply.to_ascii_lowercase().contains("<hr"),
+ "horizontal rule was kept: {reply}"
+ );
+ assert!(
+ !reply.contains("divRplyFwdMsg"),
+ "reply header was kept: {reply}"
+ );
+ assert!(
+ !reply.contains("Quoted body"),
+ "quoted thread was kept: {reply}"
+ );
+ assert!(
+ !reply.contains("Even older"),
+ "nested quote was kept: {reply}"
+ );
+}
+
+/// Outlook sometimes centers the rule in a div immediately before the header.
+#[test]
+fn outlook_reply_cuts_at_a_wrapped_horizontal_rule() {
+ let full_email = r#"
+<div>New reply</div>
+<div align="center" style="text-align:center"><hr size="2" width="98%" align="center"></div>
+<div id="divRplyFwdMsg"><b>From:</b> A<br><b>Sent:</b> Tue<br><b>To:</b> B<br><b>Subject:</b> C</div>
+<div>Quoted thread</div>
+"#;
+ let reply = compute_body_replyless(None, Some(full_email), None).expect("html body");
+
+ assert!(reply.contains("New reply"), "{reply}");
+ assert!(!reply.to_ascii_lowercase().contains("<hr"), "{reply}");
+ assert!(!reply.contains("Quoted thread"), "{reply}");
+}
+
+/// The same header without a rule still starts the quote.
+#[test]
+fn outlook_reply_header_without_a_rule_is_the_cut() {
+ let full_email = r#"
+<div>New reply</div>
+<div id="divRplyFwdMsg"><b>From:</b> A<br><b>Sent:</b> Tue<br><b>To:</b> B<br><b>Subject:</b> C</div>
+<p>Quoted thread</p>
+"#;
+ let reply = compute_body_replyless(None, Some(full_email), None).expect("html body");
+
+ assert!(reply.contains("New reply"), "{reply}");
+ assert!(!reply.contains("Quoted thread"), "{reply}");
+ assert!(!reply.contains("divRplyFwdMsg"), "{reply}");
+}
+
+/// A rule that is part of the reply stays when it does not introduce a quote.
+#[test]
+fn horizontal_rule_inside_the_reply_is_kept() {
+ let html = "<div>See the break</div><hr><div>More of the reply</div>";
+ let reply = compute_body_replyless(None, Some(html), None).expect("html body");
+
+ assert!(reply.to_ascii_lowercase().contains("<hr"), "{reply}");
+ assert!(reply.contains("More of the reply"), "{reply}");
+}
+
+#[test]
+fn plaintext_cuts_at_outlook_underscore_separator() {
+ let text = "See you then.\n\nEric\n________________________________\nFrom: Eric <[email protected]>\nSent: Thursday\n\nOlder note";
+ assert_eq!(
+ compute_body_replyless(None, None, Some(text)).as_deref(),
+ Some("See you then.\n\nEric")
+ );
+}
+
+#[test]
+fn plaintext_keeps_underscore_lines_that_are_not_reply_separators() {
+ let text = "Intro\n__________\nStill the message";
+ assert_eq!(
+ compute_body_replyless(None, None, Some(text)).as_deref(),
+ Some(text)
+ );
+}
+
/// the entire email (including style tags before the body) should be used if no splitter
/// is found on the email
#[test]