diff --git a/conf/settings.py b/conf/settings.py index 7f149138..96db6469 100644 --- a/conf/settings.py +++ b/conf/settings.py @@ -89,6 +89,7 @@ INCOMING_EMAIL_HOSTNAME = env("INCOMING_EMAIL_HOSTNAME", default="") INCOMING_EMAIL_USER = env("INCOMING_EMAIL_USER", default="") INCOMING_EMAIL_POP3_PORT = env("INCOMING_EMAIL_POP3_PORT", default=None) +INCOMING_EMAIL_CHECK_LIMIT = env.int("INCOMING_EMAIL_CHECK_LIMIT", default=100) HMRC_TO_DIT_EMAIL_PASSWORD = env("HMRC_TO_DIT_EMAIL_PASSWORD", default="") HMRC_TO_DIT_EMAIL_HOSTNAME = env("HMRC_TO_DIT_EMAIL_HOSTNAME", default="") diff --git a/mail/libraries/mailbox_service.py b/mail/libraries/mailbox_service.py index 08918460..2ce511f2 100644 --- a/mail/libraries/mailbox_service.py +++ b/mail/libraries/mailbox_service.py @@ -70,13 +70,17 @@ def get_message_iterator(pop3_connection: POP3_SSL, username: str) -> Iterator[T mails: list _, mails, _ = pop3_connection.list() mailbox_config, _ = MailboxConfig.objects.get_or_create(username=username) + incoming_email_check_limit = settings.INCOMING_EMAIL_CHECK_LIMIT - # Check only the last 500 emails + # Check only the emails specified in the setting # Since we don't delete emails from these mailboxes the total number can be very high over a perio # and increases the processing time. # The mails is a list of message number and size - message number is an increasing value so the # latest emails will always be at the end. - mail_message_ids = [get_message_id(pop3_connection, m.decode(settings.DEFAULT_ENCODING)) for m in mails[-500:]] + mail_message_ids = [ + get_message_id(pop3_connection, m.decode(settings.DEFAULT_ENCODING)) + for m in mails[-incoming_email_check_limit:] + ] # these are mailbox message ids we've seen before read_messages = get_read_messages(mailbox_config)