mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
Add attachment handling
This commit is contained in:
parent
fc53a8203b
commit
e3cd913fe7
1 changed files with 25 additions and 0 deletions
|
|
@ -3,9 +3,12 @@ from email.utils import formatdate
|
||||||
import rfc822
|
import rfc822
|
||||||
import urllib
|
import urllib
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import os
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.mail.message import make_msgid
|
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.db import models
|
||||||
from django_mailbox.transports import Pop3Transport, ImapTransport,\
|
from django_mailbox.transports import Pop3Transport, ImapTransport,\
|
||||||
MaildirTransport, MboxTransport, BabylTransport, MHTransport, \
|
MaildirTransport, MboxTransport, BabylTransport, MHTransport, \
|
||||||
|
|
@ -160,6 +163,23 @@ class Mailbox(models.Model):
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
msg.save()
|
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
|
return msg
|
||||||
|
|
||||||
def get_new_mail(self):
|
def get_new_mail(self):
|
||||||
|
|
@ -226,6 +246,8 @@ class Message(models.Model):
|
||||||
null=True,
|
null=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
attachments = models.ManyToManyField(MessageAttachment)
|
||||||
|
|
||||||
objects = models.Manager()
|
objects = models.Manager()
|
||||||
unread_messages = UnreadMessageManager()
|
unread_messages = UnreadMessageManager()
|
||||||
incoming_messages = IncomingMessageManager()
|
incoming_messages = IncomingMessageManager()
|
||||||
|
|
@ -290,3 +312,6 @@ class Message(models.Model):
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.subject
|
return self.subject
|
||||||
|
|
||||||
|
class MessageAttachment(models.Model):
|
||||||
|
document = models.FileField(upload_to='mailbox_attachments/%Y/%m/%d/')
|
||||||
Loading…
Add table
Add a link
Reference in a new issue