diff --git a/app/plugins/builtins/mail/services.py b/app/plugins/builtins/mail/services.py index 517ec61..8bd9c01 100644 --- a/app/plugins/builtins/mail/services.py +++ b/app/plugins/builtins/mail/services.py @@ -636,7 +636,9 @@ async def imap_sync_folder( ) ).scalar_one_or_none() - # Also check by message_id — mail may have been moved to Trash by IMAP server + # Also check by message_id — but ONLY within the same folder + # to prevent cross-folder moves. A mail in Trash and INBOX with + # the same message_id are separate copies (like real mail clients). if not existing_mail: # Fetch headers first to get Message-ID fetch_hdr = await client.uid('fetch', uid_str, '(BODY.PEEK[HEADER.FIELDS (MESSAGE-ID)])') @@ -655,6 +657,7 @@ async def imap_sync_folder( and_( Mail.account_id == account.id, Mail.message_id == peek_msg_id, + Mail.folder_id == folder.id, Mail.tenant_id == tenant_id, ) ) @@ -1163,6 +1166,7 @@ async def imap_sync_account( and_( Mail.account_id == account.id, Mail.message_id == message_id, + Mail.folder_id == folder.id, Mail.tenant_id == tenant_id, ) )