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

When receiving emails sent with Exchange, we look for some markers to identify the body of the mail and the reply (aka. previous email).

For some reasons, those markers aren't 100% reliable and sometimes, only one of them is present.

The commit 20ba54d53630b57c25fa3f325b0f219581314936 introduced the bug because the `HTML_EXTRACTERS` regex for exchange looks for either `messageBodySection` or `messageReplySection` but we were only using the `reply` section. So if an email had only the `body` section, it would not be correctly extracted.

This commit handle the cases where either one of them is missing and use the other one as the actual "reply". When both are present, it correctly elides the "reply" section.
This commit is contained in:
Régis Hanol
2024-12-31 15:29:36 +01:00
committed by GitHub
parent 9497a6165f
commit d523c37057
5 changed files with 97 additions and 7 deletions

View File

@ -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)