From e3cd913fe7e69d1a97f027098006044a2ed6b661 Mon Sep 17 00:00:00 2001 From: Patrick Craston Date: Tue, 15 Jan 2013 12:05:04 +0000 Subject: [PATCH] Add attachment handling --- django_mailbox/models.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 6c54192..5f0edaa 100755 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -3,9 +3,12 @@ from email.utils import formatdate import rfc822 import urllib import urlparse +import os from django.conf import settings from django.core.mail.message import make_msgid +from django.core.files import File +from django.core.files.temp import NamedTemporaryFile from django.db import models from django_mailbox.transports import Pop3Transport, ImapTransport,\ MaildirTransport, MboxTransport, BabylTransport, MHTransport, \ @@ -160,6 +163,23 @@ class Mailbox(models.Model): except IndexError: pass msg.save() + if message.is_multipart(): + for part in message.walk(): + if part.get_content_maintype() == 'multipart': + continue + if part.get('Content-Disposition') is None: + continue + filename = part.get_filename() + data = part.get_payload(decode=True) + if not data: + continue + temp_file = NamedTemporaryFile(delete=True) + temp_file.write(data) + temp_file.flush() + attachment = MessageAttachment() + attachment.document.save(filename, File(temp_file)) + attachment.save() + msg.attachments.add(attachment) return msg def get_new_mail(self): @@ -226,6 +246,8 @@ class Message(models.Model): null=True, ) + attachments = models.ManyToManyField(MessageAttachment) + objects = models.Manager() unread_messages = UnreadMessageManager() incoming_messages = IncomingMessageManager() @@ -290,3 +312,6 @@ class Message(models.Model): def __unicode__(self): return self.subject + +class MessageAttachment(models.Model): + document = models.FileField(upload_to='mailbox_attachments/%Y/%m/%d/') \ No newline at end of file