fix(mail): decode MIME-encoded attachment filenames during sync

- Decode =?utf-8?q?...?= filenames before sanitizing for both new and existing emails
- Prevents filenames like __utf-8_q_Wire_236095209372_2Epdf_2Ehtml__
- Uses email.header.decode_header + make_header for proper decoding
This commit is contained in:
Agent Zero
2026-07-16 23:23:27 +02:00
parent 1701361e92
commit 0c310c5028
+17 -4
View File
@@ -772,13 +772,19 @@ async def imap_sync_account(
if existing_att_count == 0:
for att_data in attachments:
try:
raw_filename = att_data["filename"] or "attachment"
try:
from email.header import decode_header, make_header
decoded_filename = str(make_header(decode_header(raw_filename)))
except Exception:
decoded_filename = raw_filename
storage_path = await _save_attachment_to_storage(
existing_mail.id, att_data["filename"], att_data["content"]
existing_mail.id, decoded_filename, att_data["content"]
)
attachment = MailAttachment(
tenant_id=tenant_id,
mail_id=existing_mail.id,
filename=_sanitize_filename(att_data["filename"]),
filename=_sanitize_filename(decoded_filename),
mime_type=att_data["mime_type"],
size_bytes=att_data["size"],
storage_path=storage_path,
@@ -823,13 +829,20 @@ async def imap_sync_account(
# Save attachments to storage and DB
for att_data in attachments:
try:
# Decode MIME-encoded filenames (e.g. =?utf-8?q?...?=)
raw_filename = att_data["filename"] or "attachment"
try:
from email.header import decode_header, make_header
decoded_filename = str(make_header(decode_header(raw_filename)))
except Exception:
decoded_filename = raw_filename
storage_path = await _save_attachment_to_storage(
mail.id, att_data["filename"], att_data["content"]
mail.id, decoded_filename, att_data["content"]
)
attachment = MailAttachment(
tenant_id=tenant_id,
mail_id=mail.id,
filename=_sanitize_filename(att_data["filename"]),
filename=_sanitize_filename(decoded_filename),
mime_type=att_data["mime_type"],
size_bytes=att_data["size"],
storage_path=storage_path,