From e5c4b1b1c2b9170abb385fcf52ec89ae5b83a128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 19 Oct 2024 16:24:29 +0200 Subject: [PATCH] Use default email policy to parse raw email The default policy uses the EmailMessage class instead of the Message class from the compat32 policy. The EmailMessage decodes automatically the header values. Closes #13607 --- modules/inbound_email/inbound_email.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/inbound_email/inbound_email.py b/modules/inbound_email/inbound_email.py index 94ff413d54..a22d5211f3 100644 --- a/modules/inbound_email/inbound_email.py +++ b/modules/inbound_email/inbound_email.py @@ -7,7 +7,8 @@ import json import re import urllib import uuid -from email.utils import getaddresses, parseaddr +from email.policy import default as email_policy_default +from email.utils import getaddresses from functools import partial from trytond.config import config @@ -183,17 +184,19 @@ class Email(ModelSQL, ModelView): def _as_dict(self, raw): value = {} if isinstance(raw, str): - message = email.message_from_string(raw) + message = email.message_from_string( + raw, policy=email_policy_default) else: - message = email.message_from_bytes(raw) + message = email.message_from_bytes( + raw, policy=email_policy_default) if 'From' in message: - value['from'] = parseaddr(message.get('From'))[1] + value['from'] = getaddresses([message.get('From')])[0][1] for key in ['To', 'Cc', 'Bcc']: if key in message: value[key.lower()] = [ a for _, a in getaddresses(message.get_all(key))] if 'Subject' in message: - value['subject'] = message.get('Subject') + value['subject'] = message['Subject'] text = _email_text(message) if text is not None: value['text'] = text