From 071cfcaeb78d88e384e2d9bdb90f564100c4660b Mon Sep 17 00:00:00 2001 From: Parthiv Patel Date: Mon, 20 Jun 2022 08:34:39 +0000 Subject: [PATCH] [PATCH] Upstream patch - 20062022 --- addons/mail/models/mail_thread.py | 21 +++++++-------------- addons/test_mail/data/test_mail_data.py | 19 ------------------- addons/test_mail/tests/test_mail_gateway.py | 5 ----- 3 files changed, 7 insertions(+), 38 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index b96323afd8..7f624c8ec2 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1250,14 +1250,6 @@ class MailThread(models.AbstractModel): body = etree.tostring(root, pretty_print=False, encoding='unicode') return {'body': body, 'attachments': attachments} - def _part_get_content_decoded(self, part): - try: - return part.get_content(errors='strict') - except TypeError: # no "errors" argument on the underlyding content manager - return tools.ustr(part.get_content()) - except UnicodeDecodeError: - return part.get_payload(decode=True).decode(errors='replace') - def _message_parse_extract_payload(self, message, save_original=False): """Extract body as HTML and attachments from the mail message""" attachments = [] @@ -1273,7 +1265,7 @@ class MailThread(models.AbstractModel): # type="text/html" if message.get_content_maintype() == 'text': encoding = message.get_content_charset() - body = self._part_get_content_decoded(message) + body = message.get_content() body = tools.ustr(body, encoding, errors='replace') if message.get_content_type() == 'text/plain': # text/plain ->
@@ -1296,21 +1288,22 @@ class MailThread(models.AbstractModel):
                 # 0) Inline Attachments -> attachments, with a third part in the tuple to match cid / attachment
                 if filename and part.get('content-id'):
                     inner_cid = part.get('content-id').strip('><')
-                    attachments.append(self._Attachment(filename, self._part_get_content_decoded(part), {'cid': inner_cid}))
+                    attachments.append(self._Attachment(filename, part.get_content(), {'cid': inner_cid}))
                     continue
                 # 1) Explicit Attachments -> attachments
                 if filename or part.get('content-disposition', '').strip().startswith('attachment'):
-                    attachments.append(self._Attachment(filename or 'attachment', self._part_get_content_decoded(part), {}))
+                    attachments.append(self._Attachment(filename or 'attachment', part.get_content(), {}))
                     continue
                 # 2) text/plain -> 
                 if part.get_content_type() == 'text/plain' and (not alternative or not body):
-                    body = tools.append_content_to_html(body, self._part_get_content_decoded(part), preserve=True)
+                    body = tools.append_content_to_html(body, tools.ustr(part.get_content(),
+                                                                         encoding, errors='replace'), preserve=True)
                 # 3) text/html -> raw
                 elif part.get_content_type() == 'text/html':
                     # mutlipart/alternative have one text and a html part, keep only the second
                     # mixed allows several html parts, append html content
                     append_content = not alternative or (html and mixed)
-                    html = self._part_get_content_decoded(part)
+                    html = tools.ustr(part.get_content(), encoding, errors='replace')
                     if not append_content:
                         body = html
                     else:
@@ -1319,7 +1312,7 @@ class MailThread(models.AbstractModel):
                     body = tools.html_sanitize(body, sanitize_tags=False, strip_classes=True)
                 # 4) Anything else -> attachment
                 else:
-                    attachments.append(self._Attachment(filename or 'attachment', self._part_get_content_decoded(part), {}))
+                    attachments.append(self._Attachment(filename or 'attachment', part.get_content(), {}))
 
         return self._message_parse_extract_payload_postprocess(message, {'body': body, 'attachments': attachments})
 
diff --git a/addons/test_mail/data/test_mail_data.py b/addons/test_mail/data/test_mail_data.py
index 299d2c49ef..a979ceab7d 100644
--- a/addons/test_mail/data/test_mail_data.py
+++ b/addons/test_mail/data/test_mail_data.py
@@ -1013,22 +1013,3 @@ Remote-MTA: 10.245.192.40
 
 
 --_av-UfLe6y6qxNo54-urtAxbJQ--"""
-
-
-MAIL_WRONG_CONTENT_CHARSET = """\
-Return-Path: 
-To: gaston.lagaffe@example.com
-Received: by mail1.openerp.com (Postfix, from userid 10002)
-    id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST)
-From: spirou@example.com
-Subject: Ze Subject
-MIME-Version: 1.0
-Content-Type: multipart/alternative;
-    boundary="----=_Part_4200734_24778174.1344608186754"
-------=_Part_4200734_24778174.1344608186754
-Content-Type: text/plain; charset=US-ASCII
-Content-Transfer-Encoding: quoted-printable
-
-Tonton, ton th=C3=A9 t'a-t-il ot=C3=A9 ta toux ?
-------=_Part_4200734_24778174.1344608186754
-"""
diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py
index b239064a27..9595c5f0c9 100644
--- a/addons/test_mail/tests/test_mail_gateway.py
+++ b/addons/test_mail/tests/test_mail_gateway.py
@@ -89,11 +89,6 @@ class TestEmailParsing(TestMailCommon):
         res = self.env['mail.thread'].message_parse(self.from_string(mail))
         self.assertIn('
\nPlease call me as soon as possible this afternoon!\n\n--\nSylvie\n
', res['body']) - def test_message_parse_wrong_content_charset(self): - mail = test_mail_data.MAIL_WRONG_CONTENT_CHARSET - res = self.env['mail.thread'].message_parse(self.from_string(mail)) - self.assertIn("Tonton, ton thé t'a-t-il oté ta toux ?", res['body']) - def test_message_parse_xhtml(self): # Test that the parsing of XHTML mails does not fail self.env['mail.thread'].message_parse(self.from_string(test_mail_data.MAIL_XHTML))