我正在尝试使用Python3.4回复电子邮件。电子邮件的收件人将使用Outlook(不幸的是),Outlook必须识别回复并正确显示线程。
我现在的代码是:def send_mail_multi(headers, text, msgHtml="", orig=None):
"""
"""
msg = MIMEMultipart('mixed')
# Create message container - the correct MIME type is multipart/alternative.
body = MIMEMultipart('alternative')
for k,v in headers.items():
if isinstance(v, list):
v = ', '.join(v)
msg.add_header(k, v)
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
body.attach(MIMEText(text, 'plain'))
if msgHtml != "": body.attach(MIMEText(msgHtml, 'html'))
msg.attach(body)
if orig is not None:
msg.attach(MIMEMessage(get_reply_message(orig)))
# Fix subject
msg["Subject"] = "RE: "+orig["Subject"].replace("Re: ", "").replace("RE: ", "")
msg['In-Reply-To'] = orig["Message-ID"]
msg['References'] = orig["Message-ID"]+orig["References"].strip()
msg['Thread-Topic'] = orig["Thread-Topic"]
msg['Thread-Index'] = orig["Thread-Index"]
send_it(msg['From'], msg['To'], msg)函数get_reply_message正在删除this answer中的任何附件。
send_it函数设置邮件ID头并使用正确的SMTP配置。然后它调用smtplib.sendmail(fr, to, msg.as_string())
Outlook接收电子邮件,但无法识别/显示线程。然而,线程似乎是消息的附件(可能是由msg.attach(MIMEMessage(...))引起的)
有什么办法吗?我有没有漏掉头球?
干杯
安德烈亚斯