Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Mails consumed although not fitting consumption rule
### Description

After upgrading to the newest version a couple of days ago I experienced that emails are processed although there is no file attached to the mail fitting my mail rule. The rule has been the same for months and working well. The attachments would be .png-files.

**Email inbox:**
<img width="490" alt="Bildschirmfoto 2023-05-06 um 15 12 21" src="https://user-images.githubusercontent.com/68914047/236627379-52268090-d3ad-480f-bc30-dac993de4cba.png">

**Email contents:**
<img width="603" alt="Bildschirmfoto 2023-05-06 um 15 12 25" src="https://user-images.githubusercontent.com/68914047/236627392-a90cc08a-bb8e-4db1-bc66-11ad225efc4b.png">

**Email attachments:**
<img width="328" alt="Bildschirmfoto 2023-05-06 um 15 31 16" src="https://user-images.githubusercontent.com/68914047/236627405-c5af22c4-270a-4168-885c-72b298320fc1.png">

**Server log:**
<img width="1441" alt="Bildschirmfoto 2023-05-06 um 15 18 48" src="https://user-images.githubusercontent.com/68914047/236627417-0b8dc6ad-ee9a-4082-933d-64610961edb6.png">

**Email rule:**
<img width="1016" alt="Bildschirmfoto 2023-05-06 um 15 25 09" src="https://user-images.githubusercontent.com/68914047/236627503-d270fb50-ab4c-4aa8-b784-27fea5feb2bf.png">

Is anyone else experiencing something similar?
Thank you for your lasting efforts in making such a wonderful piece of software!

### Steps to reproduce

Nothing the user has to do to reproduce.

### Webserver logs

```bash
see screenshots
```


### Browser logs

_No response_

### Paperless-ngx version

1.14.4

### Host OS

Debian 11

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

No new changes introduces

### Other

_No response_


Discussion:

Resolving patch:
diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py
--- a/src/paperless_mail/mail.py
+++ b/src/paperless_mail/mail.py
@@ -725,11 +725,27 @@ def _process_attachments(
                     f"by paperless",
                 )
 
-        queue_consumption_tasks(
-            consume_tasks=consume_tasks,
-            rule=rule,
-            message=message,
-        )
+        if len(consume_tasks) > 0:
+            queue_consumption_tasks(
+                consume_tasks=consume_tasks,
+                rule=rule,
+                message=message,
+            )
+        else:
+            # No files to consume, just mark as processed if it wasnt by .eml processing
+            if not ProcessedMail.objects.filter(
+                rule=rule,
+                uid=message.uid,
+                folder=rule.folder,
+            ).exists():
+                ProcessedMail.objects.create(
+                    rule=rule,
+                    folder=rule.folder,
+                    uid=message.uid,
+                    subject=message.subject,
+                    received=message.date,
+                    status="PROCESSED_WO_CONSUMPTION",
+                )
 
         return processed_attachments
 


Test patch:
diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py
--- a/src/paperless_mail/tests/test_mail.py
+++ b/src/paperless_mail/tests/test_mail.py
@@ -222,7 +222,7 @@ def create_message(
         self,
         attachments: Union[int, List[_AttachmentDef]] = 1,
         body: str = "",
-        subject: str = "the suject",
+        subject: str = "the subject",
         from_: str = "noone@mail.com",
         to: Optional[List[str]] = None,
         seen: bool = False,
@@ -539,7 +539,6 @@ def test_filename_filter(self):
         tests = [
             ("*.pdf", ["f1.pdf", "f2.pdf", "f3.pdf", "file.PDf", "f1.Pdf"]),
             ("f1.pdf", ["f1.pdf", "f1.Pdf"]),
-            ("f1", []),
             ("*", ["f1.pdf", "f2.pdf", "f3.pdf", "f2.png", "file.PDf", "f1.Pdf"]),
             ("*.png", ["f2.png"]),
         ]
@@ -564,6 +563,48 @@ def test_filename_filter(self):
                     ],
                 )
 
+    def test_filename_filter_inline_no_consumption(self):
+        """
+        GIVEN:
+            - Rule that processes all attachments but filters by filename
+        WHEN:
+            - Given email with inline attachment that does not meet filename filter
+        THEN:
+            - Mail action should not be performed
+        """
+        message = self.create_message(
+            attachments=[
+                _AttachmentDef(
+                    filename="test.png",
+                    disposition="inline",
+                ),
+            ],
+        )
+        self.bogus_mailbox.messages.append(message)
+        account = MailAccount.objects.create(
+            name="test",
+            imap_server="",
+            username="admin",
+            password="secret",
+        )
+        account.save()
+        rule = MailRule(
+            name=str(uuid.uuid4()),
+            assign_title_from=MailRule.TitleSource.FROM_FILENAME,
+            account=account,
+            filter_attachment_filename="*.pdf",
+            attachment_type=MailRule.AttachmentProcessing.EVERYTHING,
+            action=MailRule.MailAction.DELETE,
+        )
+        rule.save()
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 4)
+
+        self.mail_account_handler.handle_mail_account(account)
+        self.apply_mail_actions()
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 1)
+
     def test_handle_mail_account_mark_read(self):
         account = MailAccount.objects.create(
             name="test",