Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Workflow trigger ignores mail rule filter when processing full mail as .eml
### Description

A workflow that applies to only documents from a certain mailrule gets incorrectly applied to documents from a different mailrule. Occurs on documents where an email gets converted to a pdf. I can reproduce the issue. It only seems to occur for emails that are processed as .eml . 

Note: In the log section you will see two workflow triggers that incorrectly match the document. I have two very similar workflows that only differ in a filename filter. The filename filter does not match the mail therefor the second workflow gets applied in the end. This does not appear in the logs. Not sure why

# The rules in order
This is the correct mail rule and It gets applied first
![grafik](https://github.com/paperless-ngx/paperless-ngx/assets/46402226/0c9b14ca-0329-4212-8c2b-7a8ef38a5b9d)
Afterwards this workflow gets triggered. It should only apply to the mail rule "Stadtwerke Rechnungen" but for some reason gets applied to the Mail Rule "Amazon Ordine" aswell.
![grafik](https://github.com/paperless-ngx/paperless-ngx/assets/46402226/d91ad624-34cd-433a-a583-25e6c21a43be)






### Steps to reproduce

1. Create a mail rule that converts incoming mail to pdfs using the Consumption Scope "process message as .eml"
2. Create a workflow that triggers on Consumption Started, filters the source "Mail Fetch" and applies another mail rule, not the one created in Step 1. 
3. Send a mail to yourself that triggers the Rule created in Step 1. If it also triggers the workflow then the issue has been reproduced.

### Webserver logs

```bash
Logs from celery.log, mail.log and paperless.log

mail.log. Rule gets applied

[2024-02-16 13:50:01,546] [DEBUG] [paperless_mail] Rule Jonas Linter Gmail.Amazon ordine: Selecting folder INBOX/Amazon
[2024-02-16 13:50:01,733] [DEBUG] [paperless_mail] Rule Jonas Linter Gmail.Amazon ordine: Searching folder with criteria (SINCE 17-Jan-2024 UNSEEN SUBJECT "Il tuo ordine Amazon.it di")
[2024-02-16 13:50:02,382] [DEBUG] [paperless_mail] Rule Jonas Linter Gmail.Amazon ordine: Processing mail Il tuo ordine Amazon.it di "AAOTOKK USB 3.0 A Femmina a..." è stato spedito. from conferma-spedizione@amazon.it with 0 attachment(s)
[2024-02-16 13:50:02,388] [INFO] [paperless_mail] Rule Jonas Linter Gmail.Amazon ordine: Consuming eml from mail Il tuo ordine Amazon.it di "AAOTOKK USB 3.0 A Femmina a..." è stato spedito. from conferma-spedizione@amazon.it
[2024-02-16 13:50:02,445] [DEBUG] [paperless_mail] Rule Jonas Linter Gmail.Amazon ordine: Processed 1 matching mail(s)

Celery.log where the .eml is received. Notable is the mailrule_id=None. Other consumed mails have the mailrule set in this log though this may be unrelated

[2024-02-16 13:50:02,446] [INFO] [celery.worker.strategy] Task documents.tasks.consume_file[32becd67-be9e-404f-a151-3972919575b1] received
[2024-02-16 13:50:05,873] [INFO] [celery.app.trace] Task paperless_mail.tasks.process_mail_accounts[0b948b5e-3188-4349-9218-58ab221d36a8] succeeded in 5.869246774993371s: 'Added 1 document(s).'
[2024-02-16 13:50:05,880] [DEBUG] [celery.pool] TaskPool: Apply <function fast_trace_task at 0x1535ef66c2c0> (args:('documents.tasks.consume_file', '32becd67-be9e-404f-a151-3972919575b1', {'lang': 'py', 'task': 'documents.tasks.consume_file', 'id': '32becd67-be9e-404f-a151-3972919575b1', 'shadow': None, 'eta': None, 'expires': None, 'group': '0e04c58f-bf0b-4256-9da8-5445d0194547', 'group_index': 0, 'retries': 0, 'timelimit': [None, None], 'root_id': '0b948b5e-3188-4349-9218-58ab221d36a8', 'parent_id': '0b948b5e-3188-4349-9218-58ab221d36a8', 'argsrepr': '(ConsumableDocument(source=<DocumentSource.MailFetch: 3>, original_file=PosixPath(\'/tmp/paperless/paperless-mail-bzhi1c34.eml\'), mailrule_id=None, mime_type=\'message/rfc822\'), DocumentMetadataOverrides(filename=\'Il tuo ordine Amazon.it di AAOTOKK USB 3.0 A Femmina a... è stato spedito..eml\', title=\'Il tuo ordine Amazon.it di "AAOTOKK USB 3.0 A Femmina a..." è stato spedito.\', correspondent_id=16, document_type_id=12, tag_ids=[], storage_path_id=None, created=None, asn=None, owner_id=4, view_users=None, view_groups=None, change_users=None, change_groups=None,... kwargs:{})
[2024-02-16 13:50:12,856] [INFO] [celery.app.trace] Task documents.tasks.consume_file[32becd67-be9e-404f-a151-3972919575b1] succeeded in 6.7813629590091296s: 'Success. New document id 359 created'

Paperless.log: The workflow is incorrectly applied. Sadly it does not mention what exactly caused it to match.

[2024-02-16 13:50:06,114] [DEBUG] [paperless.tasks] Executing plugin WorkflowTriggerPlugin
[2024-02-16 13:50:06,118] [INFO] [paperless.matching] Document did not match Workflow: Jonas Consumption Template
[2024-02-16 13:50:06,118] [DEBUG] [paperless.matching] ('Document path /tmp/paperless/paperless-mail-bzhi1c34.eml does not match */consume/jonas/*',)
[2024-02-16 13:50:06,128] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Stadtwerke Rechnungen Fernwärme Citynet etc
[2024-02-16 13:50:06,136] [INFO] [paperless.matching] Document matched WorkflowTrigger 7 from Workflow: Stadtwerke Stromrechnung
[2024-02-16 13:50:06,141] [INFO] [paperless.tasks] WorkflowTriggerPlugin completed with no message
[2024-02-16 13:50:06,146] [INFO] [paperless.consumer] Consuming Il tuo ordine Amazon.it di AAOTOKK USB 3.0 A Femmina a... è stato spedito..eml
[2024-02-16 13:50:06,149] [DEBUG] [paperless.consumer] Detected mime type: message/rfc822
[2024-02-16 13:50:06,263] [DEBUG] [paperless.consumer] Parser: MailDocumentParser
[2024-02-16 13:50:06,265] [DEBUG] [paperless.consumer] Parsing Il tuo ordine Amazon.it di AAOTOKK USB 3.0 A Femmina a... è stato spedito..eml...
[2024-02-16 13:50:06,265] [DEBUG] [paperless.parsing.mail] Parsing file Il tuo ordine Amazon.it di AAOTOKK USB 3.0 A Femmina a... è stato spedito..eml into an email
```


### Browser logs

_No response_

### Paperless-ngx version

2.5.3

### Host OS

Linux 6.1.36-Unraid x86_64

### Installation method

Docker - official image

### Browser

Firefox

### Configuration changes

_No response_

### Other

_No response_

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.


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
@@ -831,6 +831,7 @@ def _process_eml(
         input_doc = ConsumableDocument(
             source=DocumentSource.MailFetch,
             original_file=temp_filename,
+            mailrule_id=rule.pk,
         )
         doc_overrides = DocumentMetadataOverrides(
             title=message.subject,


Test patch: