我怎样才能发送一封附加了文件名包含unicode字符的电子邮件?在
到目前为止,文件将到达,但文件名为“noname”。在
这一部分对于ASCII文件名非常有效:
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)
添加一个包含编码、语言和编码字符串的元组,而不仅仅是文件名。在 第二次尝试: 按如下方式全局设置字符集: 第三次尝试 使用 第四次尝试 使用 有什么想法吗?在 我是否遗漏了RFC2231文件名字符编码的一些重要内容?在 我使用Gmail的SMTP服务器和Python2.7。在from email import Charset
Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
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))
urllib.quote()
对文件名进行url编码。这与第三种方法对文件名的影响相同。在utf8filename = urllib.quote(os.path.basename(f).encode('utf-8'))
part.add_header('Content-Disposition', 'attachment', filename=('utf-8', 'fr', utf8filename))
与其这样告诉服务器它是UTF-8:
…当我不告诉我就发送UTF-8时,它就起作用了:
^{pr2}$文件名将正确显示。在
这似乎与documentation的说法相矛盾:
这不起作用,但是python 3 documentation添加了:。在
只有这样才行得通,即使对于Python2.7也是如此,尽管文档中没有提到它。在
相关问题 更多 >
编程相关推荐