[英]Forward email including headers using Python
我写了一个脚本来转发来自“John Smith”的主题为“ABC”的电子邮件。 在我转发 email“今天是美好的一天”之前,我还添加了自己的文本,但是问题是标题(从:,到:,主题等)在我发送时不会出现在转发的 email 中。 感谢是否有人可以提出解决方案,以便我可以转发带有标题的 email。
import win32com.client
import time
import datetime as dt
from os import path
if not path.exists('testfile_cut.txt'):
f = open("testfile_cut.txt", "w")
f.write("07/17/20 00:00:00")
f.close()
date_time = dt.datetime.now()
f = open("testfile_cut.txt", "r")
a = f.read()
a = dt.datetime.strptime(a, '%m/%d/%y %H:%M:%S')
today = dt.date.today()
#date = str(datetime.now().date())[-5].replace('-','')
lastDayDateTime = dt.datetime.now() - a
lastDayDateTime = date_time - lastDayDateTime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
sender = "john smith"
sender = sender.lower()
messages.Sort("[ReceivedTime]", True)
lastDayMessages = messages.Restrict("[ReceivedTime] >= '" + lastDayDateTime.strftime('%m/%d/%Y %H:%M %p')+"'")
f = open("testfile_cut.txt","r")
a = f.read()
print(a)
b = lastDayMessages[0].ReceivedTime
b = dt.datetime.strftime(b, '%m/%d/%y %H:%M:%S')
print(b)
if (b!=a):
for message in lastDayMessages:
if sender in message.SenderName.lower():
if message.Subject.startswith("ABC"):
print (message.ReceivedTime)
print (message.SenderName.encode('ascii', 'ignore'))
print (message.Subject.encode('ascii', 'ignore'))
print (message.Body.encode('ascii', 'ignore'))
NewMsg = message.Forward()
NewMsg.To = "Roger Smith"
OrgBody = message.Body
NewMsg.Body = "today is a good day" + str(OrgBody)
NewMsg.Subject = "Activity as of - " + today.strftime("%m/%d/%y")
NewMsg.Send()
f.close()
fl = open("testfile_cut.txt","w")
fl.write(b)
fl.close()
print ("Finished")
您正在覆盖 Body 属性 - Forward() 返回一条消息,其中 Body 预先填充了原始正文和消息头:
NewMsg.Body = "today is a good day\r\n" + str(NewMsg.Body)
您可能还希望避免循环并更新您的限制以包括对主题的限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.