python - 使用 Python 转发包含标题的电子邮件
问题描述
我写了一个脚本来转发来自“John Smith”的主题为“ABC”的电子邮件。在转发电子邮件“今天是美好的一天”之前,我还添加了自己的文本,但问题是标题(发件人:、收件人:、主题等)在我发送时不会出现在转发的电子邮件中。感谢是否有人可以提出解决方案,以便我可以转发带有标题的电子邮件。
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)
您可能还希望避免循环并更新您的限制以包括对主题的限制。
推荐阅读
- tomcat - Tomcat显示旧版本的txt文件,删除工作目录,重新启动,检查......它仍然是旧文件
- docker - 如何使用 Ansible 启动 Docker 容器
- r - 如何编写嵌套的for循环,以便内部循环不会覆盖第一个循环值
- hadoop - 无法使用“hadoop fs -ls s3a://bucketname”命令从 ec2 实例访问 s3
- html - List 元素 HTML 上的选中属性
- linux - 在一个进程中从 bash 脚本运行两个命令(conda)
- arduino - 导入 PN532Interface.h 时出现“没有此类文件错误”
- javascript - 使用输入中的文本更新 div
- python-3.x - 如何更改使用 xrange 在 python 3 中运行的代码?
- python - 将我的数据框的行从个人转换为按组计数以执行树形图