mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Modifying management command to accept incoming mail from STDIN.
--HG-- branch : exim4_pipe
This commit is contained in:
parent
b7192e9149
commit
5cf07a1c7b
2 changed files with 32 additions and 14 deletions
|
|
@ -1,14 +1,28 @@
|
||||||
import email
|
import email
|
||||||
import ipdb
|
import logging
|
||||||
|
import rfc822
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from django.core.managemet.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from django_mailbox.models import Mailbox, Message
|
from django_mailbox.models import Mailbox
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
message_string = open(sys.stdin, 'r').read()
|
message = email.message_from_string(sys.stdin.read())
|
||||||
ipdb.set_trace()
|
if message:
|
||||||
#message = email.message_from_string(message_string)
|
mailbox = self.get_mailbox_for_message(message)
|
||||||
|
mailbox.process_incoming_message(message)
|
||||||
|
logger.info("Message received from %s" % message['from'])
|
||||||
|
else:
|
||||||
|
logger.warning("Message not processable.")
|
||||||
|
|
||||||
|
def get_mailbox_for_message(self, message):
|
||||||
|
email_address = rfc822.parseaddr(message['from'][1][0:255])
|
||||||
|
mailbox, created = Mailbox.objects.get_or_create(
|
||||||
|
name=email_address,
|
||||||
|
)
|
||||||
|
return mailbox
|
||||||
|
|
|
||||||
|
|
@ -89,10 +89,7 @@ class Mailbox(models.Model):
|
||||||
conn = MMDFTransport(self.location)
|
conn = MMDFTransport(self.location)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def get_new_mail(self):
|
def process_incoming_message(self, message):
|
||||||
connection = self.get_connection()
|
|
||||||
new_mail = []
|
|
||||||
for message in connection.get_message():
|
|
||||||
msg = Message()
|
msg = Message()
|
||||||
msg.mailbox = self
|
msg.mailbox = self
|
||||||
msg.subject = message['subject'][0:255]
|
msg.subject = message['subject'][0:255]
|
||||||
|
|
@ -100,8 +97,15 @@ class Mailbox(models.Model):
|
||||||
msg.from_address = rfc822.parseaddr(message['from'])[1][0:255]
|
msg.from_address = rfc822.parseaddr(message['from'])[1][0:255]
|
||||||
msg.body = message.as_string()
|
msg.body = message.as_string()
|
||||||
msg.save()
|
msg.save()
|
||||||
new_mail.append(msg)
|
|
||||||
message_received.send(sender=self, message=msg)
|
message_received.send(sender=self, message=msg)
|
||||||
|
return msg
|
||||||
|
|
||||||
|
def get_new_mail(self):
|
||||||
|
connection = self.get_connection()
|
||||||
|
new_mail = []
|
||||||
|
for message in connection.get_message():
|
||||||
|
msg = self.process_incoming_message(message)
|
||||||
|
new_mail.append(msg)
|
||||||
return new_mail
|
return new_mail
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue