Skip to content

A platform for community discussion. Free, open, simple.

FIX: correctly extract body and/or reply from exchange emails (#30512)

Régis Hanold523c3705730
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index fce8c76b82e..c3fb865acfb 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -564,10 +564,21 @@ module Email
end
def extract_from_exchange(doc)
- # Exchange is using the 'messageReplySection' class for forwarded emails
- # And 'messageBodySection' for the actual email
- elided = doc.css("div[name='messageReplySection']").remove
- to_markdown(doc.css("div[name='messageReplySection']").to_html, elided.to_html)
+ # Exchange is using 'messageReplySection' for forwarded emails and 'messageBodySection' for the actual email
+ reply = doc.css("div[name='messageReplySection']")
+ body = doc.css("div[name='messageBodySection']")
+
+ if reply.present? && body.present?
+ elided = doc.css("div[name='messageReplySection']").remove
+ body = doc.css("div[name='messageBodySection']")
+ to_markdown(body.to_html, elided.to_html)
+ elsif reply.present?
+ to_markdown(reply.to_html, "")
+ elsif body.present?
+ to_markdown(body.to_html, "")
+ else
+ to_markdown(doc.to_html, "")
+ end
end
def extract_from_apple_mail(doc)
diff --git a/spec/fixtures/emails/exchange_html_body.eml b/spec/fixtures/emails/exchange_html_body.eml
new file mode 100644
index 00000000000..0177c632592
--- /dev/null
+++ b/spec/fixtures/emails/exchange_html_body.eml
@@ -0,0 +1,14 @@
+Return-Path: <[email protected]>
+From: Foo Bar <[email protected]>
+Date: Fri, 15 Jan 2017 00:12:43 +0100
+Message-ID: <[email protected]>
+Mime-Version: 1.0
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: quoted-printable
+
+<div>
+ <div name="messageBodySection">
+ <p>This is the <b>body</b> of the email.</p>
+ </div>
+</div>
diff --git a/spec/fixtures/emails/exchange_html_body_and_reply.eml b/spec/fixtures/emails/exchange_html_body_and_reply.eml
new file mode 100644
index 00000000000..00a423a7f92
--- /dev/null
+++ b/spec/fixtures/emails/exchange_html_body_and_reply.eml
@@ -0,0 +1,17 @@
+Return-Path: <[email protected]>
+From: Foo Bar <[email protected]>
+Date: Fri, 15 Jan 2017 00:12:43 +0100
+Message-ID: <[email protected]>
+Mime-Version: 1.0
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: quoted-printable
+
+<div>
+ <div name="messageBodySection">
+ <p>This is the <b>body</b> of the email.</p>
+ </div>
+ <div name="messageReplySection">
+ <p>This is the <i>reply</i>!</p>
+ </div>
+</div>
diff --git a/spec/fixtures/emails/exchange_html_reply.eml b/spec/fixtures/emails/exchange_html_reply.eml
new file mode 100644
index 00000000000..1cb1751834e
--- /dev/null
+++ b/spec/fixtures/emails/exchange_html_reply.eml
@@ -0,0 +1,14 @@
+Return-Path: <[email protected]>
+From: Foo Bar <[email protected]>
+Date: Fri, 15 Jan 2017 00:12:43 +0100
+Message-ID: <[email protected]>
+Mime-Version: 1.0
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: quoted-printable
+
+<div>
+ <div name="messageReplySection">
+ <p>This is the <b>body !!</b> of the email.</p>
+ </div>
+</div>
diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rb
index bb5d553d02d..e6f2a88c144 100644
--- a/spec/lib/email/receiver_spec.rb
+++ b/spec/lib/email/receiver_spec.rb
@@ -415,9 +415,43 @@ RSpec.describe Email::Receiver do
it "automatically elides gmail quotes" do
SiteSetting.always_show_trimmed_content = true
expect { process(:gmail_html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a **GMAIL** reply ;)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\nThis is the *elided* part!\n\n</details>",
- )
+ expect(topic.posts.last.raw).to eq <<~MD.strip
+ This is a **GMAIL** reply ;)
+
+ <details class='elided'>
+ <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+
+ This is the *elided* part!
+
+ </details>
+ MD
+ end
+
+ it "correctly extracts body from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_body) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the **body** of the email.")
+ end
+
+ it "correctly extracts reply from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the **body !!** of the email.")
+ end
+
+ it "correctly extracts body & reply from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_body_and_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq <<~MD.strip
+ This is the **body** of the email.
+
+ <details class='elided'>
+ <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+
+ This is the *reply*!
+
+ </details>
+ MD
end
it "doesn't process email with same message-id more than once" do