From 6a54074a084f6f14d79f8becb9ee1a97c75394ae Mon Sep 17 00:00:00 2001 From: Daniele Battaglia Date: Mon, 20 Jun 2016 11:27:14 +0200 Subject: [PATCH] Added 'message/rfc822' attachment handling message/rfc822 parts were being parsed recursively instead of being just attached as eml. Added a check to avoid recursive inspection and added an elif branch to handle the message save to disk. --- django_mailbox/models.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index c1af304..73cc5ef 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -232,7 +232,8 @@ class Mailbox(models.Model): settings = utils.get_settings() new = EmailMessage() - if msg.is_multipart(): + # walk only in multipart messages which are not contained as an attachment (message/rfc822) + if msg.is_multipart() and not 'attachment' in msg.get('Content-Disposition', ''): for header, value in msg.items(): new[header] = value for part in msg.get_payload(): @@ -254,6 +255,31 @@ class Mailbox(models.Model): ) ) new.set_payload('') + # very similar to the regular attachment branch and could be refactored + elif msg.get_content_type() == 'message/rfc822' and 'attachment' in msg.get('Content-Disposition', ''): + extension = ".eml" + attachment = MessageAttachment() + # can 'message/rfc822' have anything else than one single part? if so this is not safe and needs to be handled properly + msg_part = msg.get_payload(0) + + attachment.document.save( + uuid.uuid4().hex + extension, + ContentFile( + six.BytesIO( + msg_part.as_string(unixfrom=False) # see django-mailbox issue #100 + ).getvalue() + ) + ) + attachment.message = record + for key, value in msg.items(): + attachment[key] = value + attachment.save() + + placeholder = EmailMessage() + placeholder[ + settings['attachment_interpolation_header'] + ] = str(attachment.pk) + new = placeholder elif ( ( msg.get_content_type() not in settings['text_stored_mimetypes']