使用 email.iterators._structure(msg)
,传入的信息的原始 MIME 树结构如下:
multipart/mixed
text/html (message)
application/octet-stream (attachment 1)
application/octet-stream (attachment 2)
通过GMail回复的结果如下所示:
multipart/alternative
text/plain
text/html
即它们并不像我想象中那样聪明,只是丢弃附件(很好),并提供文本和HTML版本,显式地重新构造“引用内容”。
我开始认为这也是我应该做的全部,只需回复一条简单的消息,因为在丢弃附件之后,保留原始消息没有什么意义。
尽管如此,既然我已经知道了如何做,还是可以回答我的原始问题。
首先,在原始消息中用text/plain占位符替换所有附件:
import email
original = email.message_from_string( ... )
for part in original.walk():
if (part.get('Content-Disposition')
and part.get('Content-Disposition').startswith("attachment")):
part.set_type("text/plain")
part.set_payload("Attachment removed: %s (%s, %d bytes)"
%(part.get_filename(),
part.get_content_type(),
len(part.get_payload(decode=True))))
del part["Content-Disposition"]
del part["Content-Transfer-Encoding"]
然后创建一个回复消息:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.message import MIMEMessage
new = MIMEMultipart("mixed")
body = MIMEMultipart("alternative")
body.attach( MIMEText("reply body text", "plain") )
body.attach( MIMEText("<html>reply body text</html>", "html") )
new.attach(body)
new["Message-ID"] = email.utils.make_msgid()
new["In-Reply-To"] = original["Message-ID"]
new["References"] = original["Message-ID"]
new["Subject"] = "Re: "+original["Subject"]
new["To"] = original["Reply-To"] or original["From"]
new["From"] = "me@mysite.com"
然后附上原始的 MIME 消息对象并发送:
new.attach( MIMEMessage(original) )
s = smtplib.SMTP()
s.sendmail("me@mysite.com", [new["To"]], new.as_string())
s.quit()
最终得到的结构如下:
multipart/mixed
multipart/alternative
text/plain
text/html
message/rfc822
multipart/mixed
text/html
text/plain
text/plain
或者使用 Django 更简单:
from django.core.mail import EmailMultiAlternatives
from email.mime.message import MIMEMessage
new = EmailMultiAlternatives("Re: "+original["Subject"],
"reply body text",
"me@mysite.com",
[original["Reply-To"] or original["From"]],
headers = {'Reply-To': "me@mysite.com",
"In-Reply-To": original["Message-ID"],
"References": original["Message-ID"]})
new.attach_alternative("<html>reply body text</html>", "text/html")
new.attach( MIMEMessage(original) )
new.send()
在GMail中,该结果最终显示为"---- 转发邮件 ----",这并不完全符合我的需求,但是总体思路可行,我希望这个答案能够帮助那些试图处理MIME消息的人。