python回复邮件_如何使用Python imaplib回复电子邮件并包含原始邮件?

本文介绍如何使用Python的imaplib、smtplib和email模块回复电子邮件,并在回复中包含原始邮件的内容,但不包括附件。通过遍历原始邮件的MIME结构,替换附件为文本占位符,然后创建新的回复消息,附带纯文本和HTML版本的回复正文。最后,附加原始邮件的MIME消息对象并发送邮件。
摘要由CSDN通过智能技术生成

I'm currently using imaplib to fetch email messages from a server and process the contents and attachments.

I'd like to reply to the messages with a status/error message and links to the resulting generated content on my site if they can be processed. This should include the original message but should drop any attachments (which will be large) and preferably replace them with just their filenames/sizes.

Since I'm already walking the MIME message parts, I'm assuming what I need to do is build a new MIME message tree containing a copy of the original message and delete/replace the attachment nodes.

Before I start down that path, I was hoping someone can give me some tips. Is there any kind of library function to do this? Any kind of standard behavior I should stick to?

I currently know of/am using the imaplib, smtplib and email modules and but may have missed something obvious in there. This is running in Django too, so can use anything in django.core.email if that makes it easier.

解决方案

The original MIME tree structure of the incoming message is as follows (using email.iterators._structure(msg)):

multipart/mixed

text/html (message)

application/octet-stream (attachment 1)

application/octet-stream (attachment 2)

Replying via GMail results in the following structure:

multipart/alternative

text/plain

text/html

I.e. they aren't being as smart as I thought, just discarding the attachments (good) and providing text and HTML versions that explicitly restructure the "quoted content."

I'm beginning to think that's all I should do too, just reply with a simple message as after discarding the attachments there's not much point in keeping the original message.

Still, might as well answer my original question since I've figured out how to now anyway.

First, replace all the attachments in the original message with text/plain placeholders:

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"]

Then create a reply message:

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("reply body text", "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"

Then attach the original MIME message object and send:

new.attach( MIMEMessage(original) )

s = smtplib.SMTP()

s.sendmail("me@mysite.com", [new["To"]], new.as_string())

s.quit()

The resulting structure is:

multipart/mixed

multipart/alternative

text/plain

text/html

message/rfc822

multipart/mixed

text/html

text/plain

text/plain

Or it's a bit simpler using Django:

from django.core.mail import EmailMultiAlternatives

from email.mime.message import MIMEMessage

new = EmailMultiAlternatives("Re: "+original["Subject"],

"reply body text",

"me@mysite.com", # from

[original["Reply-To"] or original["From"]], # to

headers = {'Reply-To': "me@mysite.com",

"In-Reply-To": original["Message-ID"],

"References": original["Message-ID"]})

new.attach_alternative("reply body text", "text/html")

new.attach( MIMEMessage(original) ) # attach original message

new.send()

The result ends (in GMail at least) showing the original message as "---- Forwarded message ----" which isn't quite what I was after, but the general idea works and I hope this answer helps someone trying to figure out how to fiddle with MIME messages.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值