diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-01-13 15:23:03 +0100 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-01-13 15:24:00 +0100 |
commit | 61971193bbaf7c7572a07cbb08add0c04be5ad55 (patch) | |
tree | d8953cf0890d2f5ae1cb9db126c0f8e3f5eb2d2d /service/pixelated | |
parent | 6f6f8a338ec460aea761de320aaccc25b0eb2d42 (diff) |
Fixed ignoring charset on mail content type when parsing mails.
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 20 |
1 files changed, 14 insertions, 6 deletions
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 |