]> khamidou — GitHub mirrors - flanker.git/commitdiff
Don't throw an exception for .eml attachments with broken MIME.
authorJeremy Schlatter <jeremy.schlatter@gmail.com>
Tue, 2 Dec 2014 23:37:44 +0000 (15:37 -0800)
committerJeremy Schlatter <jeremy.schlatter@gmail.com>
Tue, 2 Dec 2014 23:37:44 +0000 (15:37 -0800)
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

index b30a1b508f96a635dcf4aadac06d960600650793..816826d76cdddd24c873583ae859a9773c5b02c6 100644 (file)
@@ -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):