Python:如何将MIME生成的电子邮件保存到磁盘

Python:如何将MIME生成的电子邮件保存到磁盘,python,python-2.7,Python,Python 2.7,我使用HTML模板创建电子邮件,并在每封电子邮件上附加一个图像。在发送电子邮件之前,我想先将它们保存在磁盘上以供审阅,然后使用单独的脚本将保存的电子邮件发送出去。目前,我以以下方式生成和发送电子邮件 from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.MIMEImage import MIMEImage from email.MIMEBase import M

我使用HTML模板创建电子邮件,并在每封电子邮件上附加一个图像。在发送电子邮件之前,我想先将它们保存在磁盘上以供审阅,然后使用单独的脚本将保存的电子邮件发送出去。目前,我以以下方式生成和发送电子邮件

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase
from email import Encoders

fileLocation = 'C:\MyDocuments\myImage.png' 
attachedFile = "'attachment; filename=" + fileLocation
text = myhtmltemplate.format(**locals())

msg = MIMEMultipart('related')
msg['Subject'] = "My subject" 
msg['From'] = 'sender@email.com'
msg['To'] = 'receiver@email.com'
msg.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)

part = MIMEBase('application', "octet-stream")
part.set_payload(open(fileLocation, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', attachedFile)
msg.attach(part)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText(text, 'html')
msgAlternative.attach(msgText)

fp = open(fileLocation, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

smtpObj = smtplib.SMTP('my.smtp.net')
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从email.MIMEImage导入MIMEImage
从email.MIMEBase导入MIMEBase
从电子邮件导入编码器
fileLocation='C:\MyDocuments\myImage.png'
attachedFile=“”附件;文件名=“+文件位置
text=myhtmltemplate.format(**locals())
msg=MIMEMultipart('相关')
msg['Subject']=“我的主题”
msg['From']='sender@email.com'
msg['To']='receiver@email.com'
msg.preamble='这是MIME格式的多部分消息。'
msgAlternative=MIMEMultipart('备选方案')
附加消息(msgaltentive)
part=MIMEBase(“应用程序”、“八位字节流”)
part.set_有效负载(open(fileLocation,“rb”).read())
编码器。编码_base64(部分)
part.add_标题('Content-Disposition',attachedFile)
附加信息(部分)
msgText=MIMEText('这是可选的纯文本消息')
msgAlternative.attach(msgText)
msgText=MIMEText(文本“html”)
msgAlternative.attach(msgText)
fp=打开(文件位置“rb”)
msgImage=MIMEImage(fp.read())
fp.close()
#定义图像的ID
msgImage.add_头('Content-ID','')
msg.attach(msgImage)
smtpObj=smtplib.SMTP('my.SMTP.net')
smtpObj.sendmail(发送方、接收方、msg.as_string())
smtpObj.quit()

如何将完全相同的电子邮件保存到磁盘,而不是立即发送?

只需打开一个文件并存储原始文本即可。如果审阅者接受,只需转发文本即可

而不是:

smtpObj = smtplib.SMTP('my.smtp.net')
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
使其保存:

f = open("output_file.txt", "w+")
f.write(msg.as_string())
f.close()
稍后,每当审阅者接受文本时:

# Read the file content
f = open("output_file.txt", "r")
email_content = f.read()
f.close()
# Send the e-mail
smtpObj = smtplib.SMTP('my.smtp.net')
smtpObj.sendmail(sender, receiver, email_content )
smtpObj.quit()

请您完成最后一部分,以便代码能够实际发送已保存的电子邮件,好吗?如果我将
smtpObj=smtplib.SMTP('my.SMTP.net')smtpObj.sendmail(sender,receiver,msg.as_string())smtpObj.quit()
添加到它,我收到的电子邮件会有两个附加图像,而不是一个和一些其他附加文件ATT00001.txt和AT00002.htm。修复了电子邮件发送。谢谢。有没有一种方法可以不提交发送者和接收者的值,而是从保存的电子邮件中自动获取它们?这种方法需要一些技巧,我现在无法做到。如果答案是正确的,请把它标为正确并投票表决