From 7e5ae1febf9aefa5335a5ad53d717a365f4921e0 Mon Sep 17 00:00:00 2001 From: Damien Albert Date: Wed, 28 Nov 2018 15:39:48 +0100 Subject: [PATCH] Support folders and extraction dir with accents, and UTF8 email policy --- imap_aex.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/imap_aex.py b/imap_aex.py index 7a44680..1de76d5 100644 --- a/imap_aex.py +++ b/imap_aex.py @@ -60,7 +60,7 @@ from configparser import ConfigParser from datetime import date, datetime -from email import message_from_bytes +from email import message_from_bytes, policy from email.message import EmailMessage, Message from email.header import decode_header from imaplib import IMAP4_SSL, IMAP4, Time2Internaldate, ParseFlags @@ -532,7 +532,8 @@ def extract(self, date_def=None, fetch_all=False): new_part.set_payload("You deleted an attachment from this message. The original MIME headers for the attachment were:\n%s" % headers_str) new_part.replace_header("Content-Transfer-Encoding", "") - new_part.add_header("X-Mozilla-External-Attachment-URL", "file:///%s/%s" % (self.extract_dir.replace("\\", "/"), filename)) + url_path = "file:///%s/%s" % (self.extract_dir.replace("\\", "/"), filename) + new_part.add_header("X-Mozilla-External-Attachment-URL", url_path) new_part.add_header("X-Mozilla-Altered", 'AttachmentDetached; date=%s' % Time2Internaldate(time())) new_mail.attach(new_part) @@ -547,7 +548,7 @@ def extract(self, date_def=None, fetch_all=False): if nb_extraction > 0 and not self.extract_only and ('detach' == self.flagged_action or not is_flagged): if not self.dry_run: print(" Extracted %s attachment%s, replacing email." % (nb_extraction, "s" if nb_extraction > 1 else "")) - status, append_data = self.imap.append(folder, " ".join(flags), '', new_mail.as_bytes()) + status, append_data = self.imap.append(imaputf7encode(folder), " ".join(flags), '', new_mail.as_bytes(policy=policy.SMTPUTF8)) if status != "OK": print(" Could not append message to IMAP server.") continue