diff --git a/django_mailbox/management/commands/processincomingmessage.py b/django_mailbox/management/commands/processincomingmessage.py index e33b369..8d269b8 100644 --- a/django_mailbox/management/commands/processincomingmessage.py +++ b/django_mailbox/management/commands/processincomingmessage.py @@ -11,18 +11,27 @@ logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) class Command(BaseCommand): - def handle(self, *args, **options): + args = "<[Mailbox Name (optional)]>" + command = "Receive incoming mail via stdin" + + def handle(self, mailbox_name=None, *args, **options): message = email.message_from_string(sys.stdin.read()) if message: - mailbox = self.get_mailbox_for_message(message) + if mailbox_name: + mailbox = self.get_mailbox_by_name(mailbox_name) + else: + 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['to'])[1][0:255] + def get_mailbox_by_name(self, name): mailbox, created = Mailbox.objects.get_or_create( - name=email_address, + name=name, ) return mailbox + + def get_mailbox_for_message(self, message): + email_address = rfc822.parseaddr(message['to'])[1][0:255] + return self.get_mailbox_by_name(email_address) diff --git a/readme.rst b/readme.rst index ac42046..0617011 100755 --- a/readme.rst +++ b/readme.rst @@ -76,6 +76,8 @@ Note that there is an additional ``/`` in the above URI after the protocol; this Getting incoming mail --------------------- +If you are utilizing one of the polling methods above, you will need to periodically poll the mailbox for messages using one of the below methods. If you are receiving mail directly from a mailserver via a pipe (using the ``processincomingmessage`` management command), you need not concern yourself with this section. + In your code ............ @@ -96,7 +98,7 @@ You can easily consume incoming mail by running the management command named ``g Receiving mail directly from Exim4 or Postfix via a pipe ======================================================== -Django Mailbox's ``processincomingmessage`` management command accepts, via ``stdin``, incoming messages. You can configure Postfix or Exim4 to pipe incoming mail to this management command to import messages directly without polling. You need not configure mailbox settings when piping-in messages, mailbox entries will be automatically created matching the e-mail address to which incoming messages are sent. +Django Mailbox's ``processincomingmessage`` management command accepts, via ``stdin``, incoming messages. You can configure Postfix or Exim4 to pipe incoming mail to this management command to import messages directly without polling. You need not configure mailbox settings when piping-in messages, mailbox entries will be automatically created matching the e-mail address to which incoming messages are sent, but if you would like to specify the mailbox name, you may provide a single argument to the ``processincmingmessage`` command specifying the name of the mailbox you would like it to use (and, if neccessary, create). Receiving Mail from Exim4 -------------------------