From c1bfc9b3a8772418982faac0a682fdb1cd2c1322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ariel=20Gerardo=20R=C3=ADos?= Date: Mon, 1 Sep 2014 15:31:13 -0300 Subject: [PATCH] Extracts inner method to process multiple content types. --- django_mailbox/utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 7d884eb..ff8c6e0 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -39,3 +39,34 @@ def convert_header_to_unicode(header): DEFAULT_CHARSET, ) return unicode(header, DEFAULT_CHARSET, 'replace') + + +def get_body_from_message(message, maintype, subtype): + """ + Fetchs the body message matching main/sub content type. + """ + body = six.text_type('') + for part in message.walk(): + if part.get_content_maintype() == maintype and \ + part.get_content_subtype() == subtype: + charset = part.get_content_charset() + this_part = part.get_payload(decode=True) + if charset: + this_part = this_part.decode(charset, 'replace') + + try: + body += this_part + except ValueError: + # Since it did not declare a charset, and we + # *should* be 7-bit clean right now, let's assume it + # is ASCII. + body += this_part.decode('ascii', 'replace') + logger.warning( + 'Error encountered while decoding text ' + 'payload from an incorrectly-constructed ' + 'e-mail; payload was converted to ASCII with ' + 'replacement, but some data may not be ' + 'represented as the sender intended.' + ) + + return body