import datetime
import itertools
+import logging
import os
import re
import tempfile
A mail action that deletes mails after processing.
"""
- def post_consume(self, M, message_uid, parameter):
+ def post_consume(self, M: MailBox, message_uid: str, parameter: str):
M.delete(message_uid)
def get_criteria(self):
return {"seen": False}
- def post_consume(self, M, message_uid, parameter):
+ def post_consume(self, M: MailBox, message_uid: str, parameter: str):
M.flag(message_uid, [MailMessageFlags.SEEN], True)
def get_criteria(self):
return {"flagged": False}
- def post_consume(self, M, message_uid, parameter):
+ def post_consume(self, M: MailBox, message_uid: str, parameter: str):
M.flag(message_uid, [MailMessageFlags.FLAGGED], True)
else:
raise ValueError("This should never happen.")
- def post_consume(self, M: MailBox, message_uid, parameter):
+ def post_consume(self, M: MailBox, message_uid: str, parameter: str):
if re.search(r"gmail\.com$|googlemail\.com$", M._host):
- for uid in message_uid:
- M.client.uid("STORE", uid, "X-GM-LABELS", self.keyword)
+ M.client.uid("STORE", message_uid, "X-GM-LABELS", self.keyword)
# AppleMail
elif self.color:
raise MailError("No keyword specified.")
+def mailbox_login(mailbox: MailBox, account: MailAccount):
+ logger = logging.getLogger("paperless_mail")
+
+ try:
+
+ mailbox.login(account.username, account.password)
+
+ except UnicodeEncodeError:
+ logger.debug("Falling back to AUTH=PLAIN")
+
+ try:
+ mailbox.login_utf8(account.username, account.password)
+ except Exception as e:
+ logger.error(
+ "Unable to authenticate with mail server using AUTH=PLAIN",
+ )
+ raise MailError(
+ f"Error while authenticating account {account}",
+ ) from e
+ except Exception as e:
+ logger.error(
+ f"Error while authenticating account {account}: {e}",
+ exc_info=False,
+ )
+ raise MailError(
+ f"Error while authenticating account {account}",
+ ) from e
+
+
@shared_task
def apply_mail_action(
result: List[str],
port=account.imap_port,
security=account.imap_security,
) as M:
- M.login(username=account.username, password=account.password)
+ mailbox_login(M, account)
M.folder.set(rule.folder)
action.post_consume(M, message_uid, rule.action_parameter)
self.log("debug", f"GMAIL Label Support: {supports_gmail_labels}")
self.log("debug", f"AUTH=PLAIN Support: {supports_auth_plain}")
- try:
-
- M.login(account.username, account.password)
-
- except UnicodeEncodeError:
- self.log("debug", "Falling back to AUTH=PLAIN")
-
- try:
- M.login_utf8(account.username, account.password)
- except Exception as e:
- self.log(
- "error",
- "Unable to authenticate with mail server using AUTH=PLAIN",
- )
- raise MailError(
- f"Error while authenticating account {account}",
- ) from e
- except Exception as e:
- self.log(
- "error",
- f"Error while authenticating account {account}: {e}",
- exc_info=False,
- )
- raise MailError(
- f"Error while authenticating account {account}",
- ) from e
+ mailbox_login(M, account)
self.log(
"debug",
self.mail_account_handler.handle_mail_account(account)
self._queue_consumption_tasks_mock.assert_called_once()
- args, kwargs = self.async_task.call_args
c = Correspondent.objects.get(name="amazon@amazon.de")
- # should work
- self.assertEqual(kwargs["override_correspondent_id"], c.id)
+ self.verify_queue_consumption_tasks_call_args(
+ [
+ [
+ {"override_correspondent_id": c.id},
+ ],
+ ],
+ )
- self.async_task.reset_mock()
+ self._queue_consumption_tasks_mock.reset_mock()
self.reset_bogus_mailbox()
with mock.patch("paperless_mail.mail.Correspondent.objects.get_or_create") as m:
self.mail_account_handler.handle_mail_account(account)
- args, kwargs = self.async_task.call_args
- self.async_task.assert_called_once()
- self.assertEqual(kwargs["override_correspondent_id"], None)
+ self.verify_queue_consumption_tasks_call_args(
+ [
+ [
+ {"override_correspondent_id": None},
+ ],
+ ],
+ )
def test_filters(self):