From b9ff6ce6a688d9149ab56807a8f6de5c7b106bad Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 12 Oct 2018 16:44:25 +0200 Subject: [PATCH] fixed: enhanced decoding for SMTPUTF8 support. see #1585 --- modoboa/lib/email_utils.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/modoboa/lib/email_utils.py b/modoboa/lib/email_utils.py index dd199e2af..2ea160a91 100644 --- a/modoboa/lib/email_utils.py +++ b/modoboa/lib/email_utils.py @@ -128,9 +128,26 @@ class Email(object): # msg parameter to maintain compatibility with # modoboa_webmail.lib.imapemail.ImapEmail if header in msg: - return "".join([smart_text(v, encoding=(e or "ascii")) - for v, e in email.header.decode_header(msg[header]) - ]) + decoded_values = [] + for value, encoding in email.header.decode_header(msg[header]): + if encoding: + value = smart_text(value, encoding=encoding) + elif isinstance(value, six.binary_type): + # SMTPUTF8 fallback (most of the time) + # Address contains non ASCII chars but is not RFC2047 + # encoded... + encoding = chardet.detect(value) + try: + value = value.decode(encoding["encoding"], "replace") + except (TypeError, UnicodeDecodeError) as exc: + six.raise_from( + InternalError( + _("unable to determine encoding of string") + ), + exc + ) + decoded_values.append(value) + return "".join(decoded_values) return "" def _fetch_message(self):