forked from mirror/django-mailbox
Adding optional argument to processincomingmessages management command for allowing one to specify a mailbox name.
This commit is contained in:
parent
22b31a5c2f
commit
4ceeb4dae7
2 changed files with 17 additions and 6 deletions
|
|
@ -11,18 +11,27 @@ logger = logging.getLogger(__name__)
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
class Command(BaseCommand):
|
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())
|
message = email.message_from_string(sys.stdin.read())
|
||||||
if message:
|
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)
|
mailbox.process_incoming_message(message)
|
||||||
logger.info("Message received from %s" % message['from'])
|
logger.info("Message received from %s" % message['from'])
|
||||||
else:
|
else:
|
||||||
logger.warning("Message not processable.")
|
logger.warning("Message not processable.")
|
||||||
|
|
||||||
def get_mailbox_for_message(self, message):
|
def get_mailbox_by_name(self, name):
|
||||||
email_address = rfc822.parseaddr(message['to'])[1][0:255]
|
|
||||||
mailbox, created = Mailbox.objects.get_or_create(
|
mailbox, created = Mailbox.objects.get_or_create(
|
||||||
name=email_address,
|
name=name,
|
||||||
)
|
)
|
||||||
return mailbox
|
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)
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,8 @@ Note that there is an additional ``/`` in the above URI after the protocol; this
|
||||||
Getting incoming mail
|
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
|
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
|
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
|
Receiving Mail from Exim4
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue