Add log messages for mail errors (#727)

* adapt to starttls interface change in imap_tools
pin imap-tools version to avoid breaking changes
improve mail log

* fix unittest

* remove uneeded print and fix merge fail

* bump to next version
This commit is contained in:
phail 2022-05-03 00:54:51 +02:00 committed by GitHub
parent ec5d80585d
commit 0d298d743a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 45 deletions

View File

@ -19,7 +19,7 @@ djangorestframework = "~=3.13"
filelock = "*" filelock = "*"
fuzzywuzzy = {extras = ["speedup"], version = "*"} fuzzywuzzy = {extras = ["speedup"], version = "*"}
gunicorn = "*" gunicorn = "*"
imap-tools = "*" imap-tools = "~=0.54.0"
langdetect = "*" langdetect = "*"
pathvalidate = "*" pathvalidate = "*"
pillow = "~=9.1" pillow = "~=9.1"

View File

@ -156,58 +156,73 @@ class MailAccountHandler(LoggingMixin):
self.log("debug", f"Processing mail account {account}") self.log("debug", f"Processing mail account {account}")
total_processed_files = 0 total_processed_files = 0
try:
with get_mailbox(
account.imap_server,
account.imap_port,
account.imap_security,
) as M:
with get_mailbox(
account.imap_server,
account.imap_port,
account.imap_security,
) as M:
try:
M.login(account.username, account.password)
except UnicodeEncodeError:
self.log("debug", "Falling back to AUTH=PLAIN")
try: try:
# rfc2595 section 6 - PLAIN SASL mechanism M.login(account.username, account.password)
client: IMAP4 = M.client
encoded = (
b"\0"
+ account.username.encode("utf8")
+ b"\0"
+ account.password.encode("utf8")
)
# Assumption is the server supports AUTH=PLAIN capability
# Could check the list with client.capability(), but then what?
# We're failing anyway then
client.authenticate("PLAIN", lambda x: encoded)
# Need to transition out of AUTH state to SELECTED except UnicodeEncodeError:
M.folder.set("INBOX") self.log("debug", "Falling back to AUTH=PLAIN")
except Exception: try:
self.log( # rfc2595 section 6 - PLAIN SASL mechanism
"error", client: IMAP4 = M.client
"Unable to authenticate with mail server using AUTH=PLAIN", encoded = (
) b"\0"
raise MailError(f"Error while authenticating account {account}") + account.username.encode("utf8")
except Exception: + b"\0"
self.log("error", "Unable to authenticate with mail server") + account.password.encode("utf8")
raise MailError(f"Error while authenticating account {account}") )
# Assumption is the server supports AUTH=PLAIN capability
# Could check the list with client.capability(), but then what?
# We're failing anyway then
client.authenticate("PLAIN", lambda x: encoded)
self.log( # Need to transition out of AUTH state to SELECTED
"debug", M.folder.set("INBOX")
f"Account {account}: Processing " f"{account.rules.count()} rule(s)", except Exception:
) self.log(
"error",
for rule in account.rules.order_by("order"): "Unable to authenticate with mail server using AUTH=PLAIN",
try: )
total_processed_files += self.handle_mail_rule(M, rule) raise MailError(f"Error while authenticating account {account}")
except Exception as e: except Exception as e:
self.log( self.log(
"error", "error",
f"Rule {rule}: Error while processing rule: {e}", f"Error while authenticating account {account}: {e}",
exc_info=True, exc_info=False,
) )
raise MailError(
f"Error while authenticating account {account}",
) from e
self.log(
"debug",
f"Account {account}: Processing "
f"{account.rules.count()} rule(s)",
)
for rule in account.rules.order_by("order"):
try:
total_processed_files += self.handle_mail_rule(M, rule)
except Exception as e:
self.log(
"error",
f"Rule {rule}: Error while processing rule: {e}",
exc_info=True,
)
except MailError:
raise
except Exception as e:
self.log(
"error",
f"Error while retrieving mailbox {account}: {e}",
exc_info=False,
)
return total_processed_files return total_processed_files