Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added maximum number of email to read and multiple mailbox names to read with getmail management command #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion django_mailbox/management/commands/getmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('-mb', '--mailboxes', type=str, nargs='+', help='Write mailbox names')
parser.add_argument('-mr', '--max_read', type=int,
help='Write maximum number of email to read from each mailbox')

def handle(self, *args, **options):
logging.basicConfig(level=logging.INFO)
Mailbox.get_new_mail_all_mailboxes(args)
Mailbox.get_new_mail_all_mailboxes(options)
16 changes: 10 additions & 6 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,19 @@ def _process_save_original_message(self, message, msg):
save=False
)

def get_new_mail(self, condition=None):
def get_new_mail(self, condition=None, max_read=None):
"""Connect to this transport and fetch new messages."""
new_mail = []
connection = self.get_connection()
if not connection:
return
for message in connection.get_message(condition):
if max_read is not None and max_read <= 0:
break
msg = self.process_incoming_message(message)
if not msg is None:
if msg is not None:
if max_read is not None:
max_read -= 1
yield msg
self.last_polling = now()
if django.VERSION >= (1, 5): # Django 1.5 introduces update_fields
Expand All @@ -463,18 +467,18 @@ def get_new_mail(self, condition=None):
self.save()

@staticmethod
def get_new_mail_all_mailboxes(args=None):
def get_new_mail_all_mailboxes(options: dict):
mailboxes = Mailbox.active_mailboxes.all()
if args:
if options.get('mailboxes'):
mailboxes = mailboxes.filter(
name=' '.join(args)
name__in=options['mailboxes']
)
for mailbox in mailboxes:
logger.info(
'Gathering messages for %s',
mailbox.name
)
messages = mailbox.get_new_mail()
messages = mailbox.get_new_mail(max_read=options.get('max_read'))
for message in messages:
logger.info(
'Received %s (from %s)',
Expand Down
6 changes: 3 additions & 3 deletions docs/topics/polling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Using a cron job
----------------

You can easily consume incoming mail by running the management command named
``getmail`` (optionally with an argument of the name of the mailbox you'd like
to get the mail for).::
``getmail`` (optionally with an argument of the name(s) of the mailbox(es) you'd like
to get the mail for. You can also pass maximum number of email count to read).::

python manage.py getmail

python manage.py getmail --mailboxes mailbox1 mailbox2 --max_read 10

.. _receiving-mail-from-exim4-or-postfix:

Expand Down