]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Add log messages for mail errors (#727)
authorphail <phel@hacknology.de>
Mon, 2 May 2022 22:54:51 +0000 (00:54 +0200)
committerGitHub <noreply@github.com>
Mon, 2 May 2022 22:54:51 +0000 (15:54 -0700)
* 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

Pipfile
src/paperless_mail/mail.py

diff --git a/Pipfile b/Pipfile
index 0ab6775e6a9308e39811ea036831e5a734a0205e..c38d8323dab2d7fad5d60521a2012503a4f40d58 100644 (file)
--- a/Pipfile
+++ b/Pipfile
@@ -19,7 +19,7 @@ djangorestframework = "~=3.13"
 filelock = "*"
 fuzzywuzzy = {extras = ["speedup"], version = "*"}
 gunicorn = "*"
-imap-tools = "*"
+imap-tools = "~=0.54.0"
 langdetect = "*"
 pathvalidate = "*"
 pillow = "~=9.1"
index 2b3267aeb36a41330962898b4f84ba01af720ee1..67cc22130548e432eda9fdbcb8b2ec5d25aa9d21 100644 (file)
@@ -156,59 +156,74 @@ class MailAccountHandler(LoggingMixin):
         self.log("debug", f"Processing mail account {account}")
 
         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:
-                    # rfc2595 section 6 - PLAIN SASL mechanism
-                    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
-                    M.folder.set("INBOX")
-                except Exception:
+                    M.login(account.username, account.password)
+
+                except UnicodeEncodeError:
+                    self.log("debug", "Falling back to AUTH=PLAIN")
+                    try:
+                        # rfc2595 section 6 - PLAIN SASL mechanism
+                        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
+                        M.folder.set("INBOX")
+                    except Exception:
+                        self.log(
+                            "error",
+                            "Unable to authenticate with mail server using AUTH=PLAIN",
+                        )
+                        raise MailError(f"Error while authenticating account {account}")
+                except Exception as e:
                     self.log(
                         "error",
-                        "Unable to authenticate with mail server using AUTH=PLAIN",
+                        f"Error while authenticating account {account}: {e}",
+                        exc_info=False,
                     )
-                    raise MailError(f"Error while authenticating account {account}")
-            except Exception:
-                self.log("error", "Unable to authenticate with mail server")
-                raise MailError(f"Error while authenticating account {account}")
+                    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(
-                "debug",
-                f"Account {account}: Processing " f"{account.rules.count()} rule(s)",
+                "error",
+                f"Error while retrieving mailbox {account}: {e}",
+                exc_info=False,
             )
 
-            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,
-                    )
-
         return total_processed_files
 
     def handle_mail_rule(self, M: MailBox, rule):