diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-04-09 11:38:07 +0200 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-04-09 11:48:26 +0200 |
commit | 2b5ab82d00fb7ebe746f0df6ef66b014fd4e50f6 (patch) | |
tree | f3ef71a354812d6f53acd8a81178e9b3aaf43a06 /service/pixelated | |
parent | 009a945b9472c8a3aed9980f27396f1da33672ac (diff) |
Added fallback to us ascii for invalid Content-Type values.
- Issue #347
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 618b980a..7e0d5ad1 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -240,21 +240,28 @@ class PixelatedMail(Mail): try: decoding_map = { - 'quoted-printable': lambda content, content_type: unicode(content.decode('quopri'), content_type), + 'quoted-printable': lambda content, content_type: content.decode('quopri').decode(content_type), 'base64': lambda content, content_type: content.decode('base64').decode('utf-8'), '7bit': lambda content, content_type: content.encode(content_type), '8bit': lambda content, content_type: content.encode(content_type) } if encoding: - return decoding_map[encoding](part['content'], content_type) + return self._decode_content_with_fallback(part['content'], decoding_map[encoding], content_type) else: - return part['content'] + return self._decode_content_with_fallback(part['content'], lambda content, content_type: content.encode(content_type), 'ascii') except Exception: logger.error('Failed to decode mail part with:') logger.error('Content-Transfer-Encoding: %s' % encoding) logger.error('Content-Type: %s' % part['headers'].get('Content-Type')) raise + def _decode_content_with_fallback(self, content, decode_func, content_type): + try: + return decode_func(content, content_type) + # return content.encode(content_type) + except ValueError: + return content.encode('ascii', 'ignore') + @property def alternatives(self): return self.parts.get('alternatives') |