From 7ea4eeae861f98c1c5c98187eb5a946ca19a2a01 Mon Sep 17 00:00:00 2001 From: "Hubert Van de Walle (huvw)" Date: Fri, 20 Aug 2021 13:33:22 +0000 Subject: [PATCH] [FIX] mail: exception when sending an email with \n in the record_name Step to follow: - Create an Asset Models - Set up the Fixed Asset Account: Automate Asset -> Create in draft or Create and validate Asset Model -> The one you have just created - Create a Vendor bill Account -> the Fixed Asset Account of the asset model created Label -> insert a newline Price -> (do not forget to set a price) - Validate - Go to the asset automatically created - @ mention a user in the chatter Cause of the issue: The generated email subject comes from the record_name and it can contain newlines Email headers don't allow newlines and an exception is thrown here https://github.com/python/cpython/blob/60b93d9e4922eeae25052bc15909d1f4152babde/Lib/email/policy.py#L143 Solution Replace newlines by spaces in the email subject opw-2522055 closes odoo/odoo#77712 X-original-commit: ed343998d39ff91a3f73cc8012be498d32c91b2f Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_thread.py | 2 ++ addons/test_mail/tests/test_message_post.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 462ec514378..84be9b714a2 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2214,6 +2214,8 @@ class MailThread(models.AbstractModel): base_template = False mail_subject = message.subject or (message.record_name and 'Re: %s' % message.record_name) # in cache, no queries + # Replace new lines by spaces to conform to email headers requirements + mail_subject = ' '.join((mail_subject or '').splitlines()) # prepare notification mail values base_mail_values = { 'mail_message_id': message.id, diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index f1c970881e6..b616b60831d 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -302,6 +302,16 @@ class TestMessagePost(TestMailCommon, TestRecipients): self.assertEqual(new_msg.parent_id.id, parent_msg.id, 'message_post: flatten error') self.assertEqual(new_msg.partner_ids, self.env['res.partner']) + @mute_logger('odoo.addons.mail.models.mail_mail') + def test_post_email_with_multiline_subject(self): + _body, _body_alt, _subject = '

Test Body

', 'Test Body', '1st line\n2nd line' + msg = self.test_record.with_user(self.user_employee).message_post( + body=_body, subject=_subject, + message_type='comment', subtype_xmlid='mail.mt_comment', + partner_ids=[self.partner_1.id, self.partner_2.id] + ) + self.assertEqual(msg.subject, '1st line 2nd line') + @mute_logger('odoo.addons.mail.models.mail_mail') def test_post_portal_ok(self): self.test_record.message_subscribe((self.partner_1 | self.user_employee.partner_id).ids)