From 61971193bbaf7c7572a07cbb08add0c04be5ad55 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 13 Jan 2015 15:23:03 +0100 Subject: Fixed ignoring charset on mail content type when parsing mails. --- service/pixelated/adapter/model/mail.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'service/pixelated/adapter') diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 7984cb05..f1b7774c 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -76,10 +76,16 @@ class Mail(object): def _charset(self): if 'content_type' in self.headers and 'charset' in self.headers['content_type']: - return re.compile('.*charset=(.*)').match(self.headers['content_type']).group(1) + return self._parse_charset_heade(self.headers['content_type']) else: return 'utf-8' + def _parse_charset_header(self, charset_header, default_charset='utf-8'): + try: + return re.compile('.*charset=(.*)').match(charset_header).group(1) + except: + return default_charset + @property def raw(self): return self._mime_multipart.as_string() @@ -213,14 +219,16 @@ class PixelatedMail(Mail): def _decode_part(self, part): encoding = part['headers'].get('Content-Transfer-Encoding', '') + content_type = self._parse_charset_header(part['headers'].get('Content-Type')) decoding_map = { - 'quoted-printable': lambda content: unicode(content.decode('quopri')), - 'base64': lambda content: content.decode('base64').decode('utf-8') + 'quoted-printable': lambda content, content_type: unicode(content.decode('quopri'), content_type), + 'base64': lambda content, content_type: content.decode('base64').decode('utf-8') } if encoding: - return decoding_map[encoding](part['content']) - return part['content'] + return decoding_map[encoding](part['content'], content_type) + else: + return part['content'] @property def alternatives(self): @@ -228,7 +236,7 @@ class PixelatedMail(Mail): @property def text_plain_body(self): - if self.parts and len(self.alternatives) == 1: + if self.parts and len(self.alternatives) >= 1: return self._decode_part(self.alternatives[0]) else: return self.bdoc.content['raw'] # plain -- cgit v1.2.3