【发布时间】:2015-01-15 19:43:20
【问题描述】:
我正在设置一个脚本,将收到的邮件转发到收件人列表。
这是我现在拥有的:
我从标准输入阅读了电子邮件(这就是 postfix 传递它的方式):
email_in = sys.stdin.read()
incoming = Parser().parse(email_in)
sender = incoming['from']
this_address = incoming['to']
我测试多部分:
if incoming.is_multipart():
for payload in incoming.get_payload():
# if payload.is_multipart(): ...
body = payload.get_payload()
else:
body = incoming.get_payload(decode=True)`
我设置了外发消息:
msg = MIMEMultipart()
msg['Subject'] = incoming['subject']
msg['From'] = this_address
msg['reply-to'] = sender
msg['To'] = "foo@bar.com"
msg.attach(MIMEText(body.encode('utf-8'), 'html', _charset='UTF-8'))
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
这非常适用于 ASCII 字符(英文文本),转发它等等。
当我发送非 ascii 字符时,它会返回乱码(取决于电子邮件客户端字节或 utf-8 字符的 ascii 表示)
可能是什么问题?是输入端还是输出端?
【问题讨论】:
-
您不必为
MIMEText部分编码有效负载;它会为您编码,无论如何选择合适的字符集。并不是说这会改变输出。
标签: python email python-3.x utf-8