小编Rak*_*esh的帖子

使用非ASCII字符在Python中编码邮件主题(SMTP)

我使用Python模块MimeWriter构造消息,smtplib发送邮件构造的消息是:

file msg.txt:
-----------------------
Content-Type: multipart/mixed;
from: me<me@abc.com>
to: me@abc.com
subject: ??

Content-Type: text/plain;charset=utf-8

??
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码发送邮件:

import smtplib
s=smtplib.SMTP('smtp.abc.com')
toList = ['me@abc.com']
f=open('msg.txt') #above msg in msg.txt file
msg=f.read()
f.close()
s.sendmail('me@abc.com',toList,msg)
Run Code Online (Sandbox Code Playgroud)

我正确收到邮件正文,但主题不合适,

subject: some junk characters

??           <- body is correct.
Run Code Online (Sandbox Code Playgroud)

请指教?有没有办法指定用于主题的解码,如为正文指定的那样.如何正确解码主题?

python smtp utf-8 character-encoding cjk

18
推荐指数
2
解决办法
1万
查看次数

标签 统计

character-encoding ×1

cjk ×1

python ×1

smtp ×1

utf-8 ×1