How can I send an email with a file attached where the file name contains unicode characters?
Up to now, the file will arrive but with the filename "noname".
This is the part that works perfectly well for ASCII filenames:
import smtplib
from email.mime.text import MIMEText
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.Utils import formatdate
from email import Encoders
from email.Utils import encode_rfc2231
msg = MIMEMultipart()
msg['Subject'] = "New magazine delivery!"
msg['From'] = sender_email
msg['To'] = ', '.join(kindle_emails)
msg['Date'] = formatdate(localtime=True)
message = "see attachment"
msg.attach(MIMEText(message))
part = MIMEApplication(open(f, 'rb').read(), _subtype='application/x-mobipocket-ebook')
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(filename)
msg.attach(part)
First try
Adding a tuple of encoding, language and encoded string and not only the filename.
part.add_header('Content-Disposition', 'attachment', filename=('utf-8', 'fr', os.path.basename(f).encode('utf-8')))
Second try:
Setting the charset globally like this:
from email import Charset
Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
Third try
Using utils.encode_rfc2231
from email.Utils import encode_rfc2231
utf8filename = encode_rfc2231(os.path.basename(f).encode('utf-8'), charset='utf-8')
part.add_header('Content-Disposition', 'attachment', filename=('utf-8', 'fr', utf8filename))
Fourth try
Using urllib.quote()
to urlencode the filename. This has the same effect on the filename as the third method.
utf8filename = urllib.quote(os.path.basename(f).encode('utf-8'))
part.add_header('Content-Disposition', 'attachment', filename=('utf-8', 'fr', utf8filename))
Any ideas?
Am I missing something essential about RFC2231 filename character encoding?
I use Gmail's SMTP server and python 2.7.