[FIX] mail: mails with attachments containing special chars (such as é) were not delivered properly

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
This commit is contained in:
Denis Ledoux 2014-01-02 17:11:49 +01:00
parent e0951d6e18
commit bc50cd3558
1 changed files with 14 additions and 2 deletions

View File

@ -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 -> <pre/>
if part.get_content_type() == 'text/plain' and (not alternative or not body):