Fix #1098478 (calibre-smtp fails to attach file with UTF-8 characters)

This commit is contained in:
Kovid Goyal 2013-01-11 15:37:41 +05:30
commit b392089217

View File

@ -37,16 +37,17 @@ def create_mail(from_, to, subject, text=None, attachment_data=None,
if attachment_data is not None: if attachment_data is not None:
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.header import Header
assert attachment_data and attachment_name assert attachment_data and attachment_name
try: try:
maintype, subtype = attachment_type.split('/', 1) maintype, subtype = attachment_type.split('/', 1)
except AttributeError: except AttributeError:
maintype, subtype = 'application', 'octet-stream' maintype, subtype = 'application', 'octet-stream'
msg = MIMEBase(maintype, subtype) msg = MIMEBase(maintype, subtype, name=Header(attachment_name, 'utf-8').encode())
msg.set_payload(attachment_data) msg.set_payload(attachment_data)
encoders.encode_base64(msg) encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', msg.add_header('Content-Disposition', 'attachment',
filename=attachment_name) filename=Header(attachment_name, 'utf-8').encode())
outer.attach(msg) outer.attach(msg)
return outer.as_string() return outer.as_string()