From 99d4bc5e0420019f3ef13c8c2586f9b5e605dee3 Mon Sep 17 00:00:00 2001 From: Xavier ALT Date: Wed, 2 Aug 2023 09:33:14 +0000 Subject: [PATCH] [FIX] mail: correctly parse body as html for pure-html email Some external tools send email as pure html (no multipart) and when parsing such email we ends up having the raw HTML as body (text) This commit ensure we correctly parse and sanitize the body as HTML for such emails. closes odoo/odoo#132127 Task-id: 3451889 X-original-commit: a4763d214bfc7b09510226e552f749f9cd95870e Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Xavier Alt (xal) --- addons/mail/models/mail_thread.py | 3 +++ addons/test_mail/data/test_mail_data.py | 30 +++++++++++++++++++++ addons/test_mail/tests/test_mail_gateway.py | 6 +++++ 3 files changed, 39 insertions(+) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 373efb6eead..9fd6652da09 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1370,6 +1370,9 @@ class MailThread(models.AbstractModel): if message.get_content_type() == 'text/plain': # text/plain ->
                 body = tools.append_content_to_html('', body, preserve=True)
+            elif message.get_content_type() == 'text/html':
+                # we only strip_classes here everything else will be done in by html field of mail.message
+                body = tools.html_sanitize(body, sanitize_tags=False, strip_classes=True)
         else:
             alternative = False
             mixed = False
diff --git a/addons/test_mail/data/test_mail_data.py b/addons/test_mail/data/test_mail_data.py
index c823fa60888..c2e6fdb0454 100644
--- a/addons/test_mail/data/test_mail_data.py
+++ b/addons/test_mail/data/test_mail_data.py
@@ -105,6 +105,36 @@ Please call me as soon as possible this afternoon!
 Sylvie
 """
 
+MAIL_TEMPLATE_HTML = """Return-Path: {return_path}
+To: {to}
+cc: {cc}
+Received: by mail1.openerp.com (Postfix, from userid 10002)
+    id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST)
+From: {email_from}
+Subject: {subject}
+MIME-Version: 1.0
+Content-Type: text/html; charset=utf-8
+Content-Transfer-Encoding: quoted-printable
+Date: Fri, 10 Aug 2012 14:16:26 +0000
+Message-ID: {msg_id}
+{extra}
+
+
+
+ =20
+  
+ =20
+ =20
+
+  

Please call me as soon as possible this afternoon!

+ +

--
+ Sylvie +

+ + +""" + MAIL_MULTIPART_MIXED = """Return-Path: X-Original-To: raoul@grosbedon.fr Delivered-To: raoul@grosbedon.fr diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index 8c8ab81a3bc..8cfa50aabb9 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -44,6 +44,12 @@ class TestEmailParsing(MailCommon): res = self.env['mail.thread'].message_parse(self.from_string(plaintext)) self.assertIn('Please call me as soon as possible this afternoon!', res['body']) + # test pure html + html = self.format(test_mail_data.MAIL_TEMPLATE_HTML, email_from='"Sylvie Lelitre" ') + res = self.env['mail.thread'].message_parse(self.from_string(html)) + self.assertIn('

Please call me as soon as possible this afternoon!

', res['body']) + self.assertNotIn(' html has priority multipart = self.format(MAIL_TEMPLATE, email_from='"Sylvie Lelitre" ') res = self.env['mail.thread'].message_parse(self.from_string(multipart))