Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Enhancement: allow multiple filename attachment exclusion patterns for a mail rule (#5524)

Dennis Melzereaaaa575b8cb
diff --git a/docs/usage.md b/docs/usage.md
index a20e5ea4a..bf45d4982 100644
--- a/docs/usage.md
+++ b/docs/usage.md
@@ -136,7 +136,8 @@ These rules perform the following:
Paperless will check all emails only once and completely ignore messages
that do not match your filters. It will also only perform the rule action
-on e-mails that it has consumed documents from.
+on e-mails that it has consumed documents from. The filename attachment
+exclusion pattern can include multiple patterns separated by a comma.
The actions all ensure that the same mail is not consumed twice by
different means. These are as follows:
diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py
index 50435de5d..83771dbf5 100644
--- a/src/paperless_mail/mail.py
+++ b/src/paperless_mail/mail.py
@@ -656,6 +656,24 @@ class MailAccountHandler(LoggingMixin):
return processed_elements
+ def filename_exclusion_matches(
+ self,
+ filter_attachment_filename_exclude: Optional[str],
+ filename: str,
+ ) -> bool:
+ if filter_attachment_filename_exclude:
+ filter_attachment_filename_exclusions = (
+ filter_attachment_filename_exclude.split(",")
+ )
+
+ # Force the filename and pattern to the lowercase
+ # as this is system dependent otherwise
+ filename = filename.lower()
+ for filename_exclude in filter_attachment_filename_exclusions:
+ if filename_exclude and fnmatch(filename, filename_exclude.lower()):
+ return True
+ return False
+
def _process_attachments(
self,
message: MailMessage,
@@ -692,12 +710,10 @@ class MailAccountHandler(LoggingMixin):
f"does not match pattern {rule.filter_attachment_filename_include}",
)
continue
- elif rule.filter_attachment_filename_exclude and fnmatch(
- att.filename.lower(),
- rule.filter_attachment_filename_exclude.lower(),
+ elif self.filename_exclusion_matches(
+ rule.filter_attachment_filename_exclude,
+ att.filename,
):
- # Force the filename and pattern to the lowercase
- # as this is system dependent otherwise
self.log.debug(
f"Rule {rule}: "
f"Skipping attachment {att.filename} "
diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py
index 26130b643..0920f033c 100644
--- a/src/paperless_mail/tests/test_mail.py
+++ b/src/paperless_mail/tests/test_mail.py
@@ -592,6 +592,18 @@ class TestMail(
exclude_pattern="f1*",
expected_matches=["f2.pdf", "f3.pdf", "file.PDf"],
),
+ FilterTestCase(
+ "PDF Files without f1 and f2",
+ include_pattern="*.pdf",
+ exclude_pattern="f1*,f2*",
+ expected_matches=["f3.pdf", "file.PDf"],
+ ),
+ FilterTestCase(
+ "PDF Files without f1 and f2 and f3",
+ include_pattern="*.pdf",
+ exclude_pattern="f1*,f2*,f3*",
+ expected_matches=["file.PDf"],
+ ),
FilterTestCase(
"All Files, no PNG",
include_pattern="*",