Commit 821b2375 authored by Nicolas Delaby's avatar Nicolas Delaby

Add naïve loop to guess which encoding is used when email indicate wrong encoding

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24281 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a0355522
...@@ -129,6 +129,10 @@ class EmailDocument(File, TextDocument): ...@@ -129,6 +129,10 @@ class EmailDocument(File, TextDocument):
for (name, value) in self._getMessage().items(): for (name, value) in self._getMessage().items():
for text, encoding in decode_header(value): for text, encoding in decode_header(value):
if encoding is not None: if encoding is not None:
try:
text = text.decode(encoding).encode('utf-8')
except UnicodeDecodeError:
encoding = self._guessEncoding(text)
text = text.decode(encoding).encode('utf-8') text = text.decode(encoding).encode('utf-8')
if name in result: if name in result:
result[name] = '%s %s' % (result[name], text) result[name] = '%s %s' % (result[name], text)
...@@ -596,6 +600,20 @@ class EmailDocument(File, TextDocument): ...@@ -596,6 +600,20 @@ class EmailDocument(File, TextDocument):
""" """
self.MailHost.send(message) 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 ## Compatibility layer
#from Products.ERP5Type import Document #from Products.ERP5Type import Document
#Document.MailMessage = EmailDocument #Document.MailMessage = EmailDocument
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment