diff --git a/product/ERP5/Document/EmailDocument.py b/product/ERP5/Document/EmailDocument.py index 948d518a8e5d999cfcba676f80cb2ea09c2ca1d0..5fd708a54ae2b2272c36fba4fa58e56bbd77083c 100644 --- a/product/ERP5/Document/EmailDocument.py +++ b/product/ERP5/Document/EmailDocument.py @@ -129,7 +129,11 @@ class EmailDocument(File, TextDocument): for (name, value) in self._getMessage().items(): for text, encoding in decode_header(value): if encoding is not None: - text = text.decode(encoding).encode('utf-8') + try: + text = text.decode(encoding).encode('utf-8') + except UnicodeDecodeError: + encoding = self._guessEncoding(text) + text = text.decode(encoding).encode('utf-8') if name in result: result[name] = '%s %s' % (result[name], text) else: @@ -596,6 +600,20 @@ class EmailDocument(File, TextDocument): """ self.MailHost.send(message) + def _guessEncoding(self, string): + """ + Some Email Clients indicate wrong encoding + This method try to guess which encoding is used. + """ + from encodings.aliases import aliases + codec_list = set(aliases.values()) + for codec in codec_list: + try: + string.decode(codec) + except (UnicodeDecodeError, IOError): + continue + return codec + ## Compatibility layer #from Products.ERP5Type import Document #Document.MailMessage = EmailDocument