[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) <tde@openerp.com>
This commit is contained in:
Hubert Van de Walle (huvw)
2021-10-04 14:12:30 +00:00
committed by Thibault Delavallée
parent fe82ccc860
commit 7ea4eeae86
2 changed files with 12 additions and 0 deletions
+2
View File
@@ -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,
@@ -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 = '<p>Test Body</p>', '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)