使用Python3发送UTF-8编码的电子邮件会导致UnicodeError
我期待以下代码发送UTF-8编码的邮件使用Python3发送UTF-8编码的电子邮件会导致UnicodeError,python,email,python-3.x,encoding,smtplib,Python,Email,Python 3.x,Encoding,Smtplib,我期待以下代码发送UTF-8编码的邮件 import email.parser import smtplib msg_str = """From: A <a@example.org> To: B <b@example.org> Content-Type: text/plain; charset="UTF-8" Testing UTF-8: – ä.""" msg = email.parser.Parser().parsestr(msg_str) smtplib.SM
import email.parser
import smtplib
msg_str = """From: A <a@example.org>
To: B <b@example.org>
Content-Type: text/plain; charset="UTF-8"
Testing UTF-8: – ä."""
msg = email.parser.Parser().parsestr(msg_str)
smtplib.SMTP('localhost').send_message(msg)
这里失败的是在pythonssmtplib
中调用email.generator.BytesGenerator
。如果msg
对象不能转换为字节,那么电子邮件解析器不应该首先失败吗
UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 15: ordinal not in range(128)