From bc50cd3558de6feec3e062d2da88dc4433ddbda3 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 2 Jan 2014 17:11:49 +0100 Subject: [PATCH] =?UTF-8?q?[FIX]=20mail:=20mails=20with=20attachments=20co?= =?UTF-8?q?ntaining=20special=20chars=20(such=20as=20=C3=A9)=20were=20not?= =?UTF-8?q?=20delivered=20properly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_filename method from email lib returns sometimes decoded value, sometimes still encoded. for instance, get_filename cannot decode iso encoded filename Recently, decode method has been applied on filename string, to handle iso encoded file. Sadly, one cannot decode already decoded values. Therefore, decoding already decoded windows or utf-8 encoded filename with special chars failed. bzr revid: dle@openerp.com-20140102161149-x268xj7bzvvsit2h --- addons/mail/mail_thread.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 1c3b4dfa9fa..cf976981132 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -763,11 +763,23 @@ class mail_thread(osv.AbstractModel): alternative = True if part.get_content_maintype() == 'multipart': continue # skip container - filename = part.get_filename() # None if normal part + # part.get_filename returns decoded value if able to decode, coded otherwise. + # original get_filename is not able to decode iso-8859-1 (for instance). + # therefore, iso encoded attachements are not able to be decoded properly with get_filename + # code here partially copy the original get_filename method, but handle more encoding + filename=part.get_param('filename', None, 'content-disposition') + if not filename: + filename=part.get_param('name', None) + if filename: + if isinstance(filename, tuple): + # RFC2231 + filename=email.utils.collapse_rfc2231_value(filename).strip() + else: + filename=decode(filename) encoding = part.get_content_charset() # None if attachment # 1) Explicit Attachments -> attachments if filename or part.get('content-disposition', '').strip().startswith('attachment'): - attachments.append((decode(filename) or 'attachment', part.get_payload(decode=True))) + attachments.append((filename or 'attachment', part.get_payload(decode=True))) continue # 2) text/plain ->
                 if part.get_content_type() == 'text/plain' and (not alternative or not body):