From c00899dd19386b2c394c701996b18c20bba8d51c Mon Sep 17 00:00:00 2001 From: Jeremy Schlatter Date: Tue, 2 Dec 2014 15:37:44 -0800 Subject: [PATCH] Don't throw an exception for .eml attachments with broken MIME. This function used to fail when creating an attachment that happened to contain text with broken MIME. Now when that happens, we just encode it and call the attachment type 'text/plain'. --- flanker/mime/create.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/flanker/mime/create.py b/flanker/mime/create.py index b30a1b5..816826d 100644 --- a/flanker/mime/create.py +++ b/flanker/mime/create.py @@ -1,6 +1,7 @@ """ This package is a set of utilities and methods for building mime messages """ import uuid +from flanker.mime import DecodingError from flanker.mime.message import ContentType, utils from flanker.mime.message.part import MimePart, Body, Part, adjust_content_type from flanker.mime.message import scanner @@ -63,16 +64,18 @@ def attachment(content_type, body, filename=None, ContentType(main, sub), body, filename) if content_type.main == 'message': - message = message_container(from_string(body)) - message.headers['Content-Disposition'] = WithParams(disposition) - return message - else: - return binary( - content_type.main, - content_type.sub, - body, filename, - disposition, - charset) + try: + message = message_container(from_string(body)) + message.headers['Content-Disposition'] = WithParams(disposition) + return message + except DecodingError: + content_type = ContentType('text', 'plain') + return binary( + content_type.main, + content_type.sub, + body, filename, + disposition, + charset) def from_string(string): -- 2.43.0