gpt4 book ai didi

python - 如何查看一段时间内收到的所有邮件?

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:13 25 4
gpt4 key购买 nike

我有以下方法 get_email(),基本上每 20 秒获取一次最新的电子邮件并对其执行一系列其他方法。

def get_email():
import win32com.client
import os
import time
import datetime as dt

date_time = time.strftime('%m-%d-%Y')

outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items
message = messages.GetFirst() # any time calling GetFirst(), you can get GetNext()....
email_subject = message.subject
email_sender = message.SenderEmailAddress
attachments = message.Attachments
body_content = message.body

print ('From: ' + email_sender)
print ('Subject: ' + email_subject)

if attachments.Count > 0:
print (str(attachments.Count) + ' attachments found.')
for i in range(attachments.Count):
email_attachment = attachments.Item(i+1)
report_name = date_time + '_' + email_attachment.FileName
print('Pushing attachment - ' + report_name + ' - to check_correct_email() function.')

if check_correct_email(email_attachment, email_subject, report_name) == True:
save_incoming_report(email_attachment, report_name, get_report_directory(email_subject))
else:
print('Not the attachment we are looking for.')
# add error logging here
break

else: #***********add error logging here**************
print('No attachment found.')

我的主要问题是:

  • 有没有一种方法可以每小时使用 GetNext() 函数本身遍历每封电子邮件,而不是每 20 秒获取一次最新的电子邮件(这绝对不如搜索所有电子邮件有效)电子邮件)?

鉴于有两个函数:GetFirst()GetNext() 我如何正确地保存最新检查的,然后遍历所有那些有待检查吗?


您是否认为在 Outlook 中设置一个不同的文件夹会更容易,我可以将所有这些报告推送到其中,然后按时间循环访问它们?这里唯一的问题是,如果传入报告是自动生成的,并且电子邮件之间的时间间隔小于 20 秒,甚至 1 秒。


非常感谢任何帮助!

最佳答案

您可以使用 Restrict function将您的消息变量限制为过去一小时内发送的电子邮件,并迭代其中的每一个。 Restrict 从您的收件箱中获取完整的项目列表,并为您提供满足特定条件的项目列表,例如在指定时间范围内收到的项目。 (上面链接的 MSDN 文档列出了一些您可以限制的其他潜在属性。)

如果您每小时运行一次,您可以将收件箱限制为过去一小时内收到的邮件(大概是仍需要搜索的邮件)并迭代这些邮件。

这是一个限制在过去一小时(或一分钟)内收到的电子邮件的示例:

import win32com.client
import os
import time
import datetime as dt

# this is set to the current time
date_time = dt.datetime.now()
# this is set to one hour ago
lastHourDateTime = dt.datetime.now() - dt.timedelta(hours = 1)
#This is set to one minute ago; you can change timedelta's argument to whatever you want it to be
lastMinuteDateTime = dt.datetime.now() - dt.timedelta(minutes = 1)

outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = outlook.GetDefaultFolder(6)

# retrieve all emails in the inbox, then sort them from most recently received to oldest (False will give you the reverse). Not strictly necessary, but good to know if order matters for your search
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)

# restrict to messages from the past hour based on ReceivedTime using the dates defined above.
# lastHourMessages will contain only emails with a ReceivedTime later than an hour ago
# The way the datetime is formatted DOES matter; You can't add seconds here.
lastHourMessages = messages.Restrict("[ReceivedTime] >= '" +lastHourDateTime.strftime('%m/%d/%Y %H:%M %p')+"'")

lastMinuteMessages = messages.Restrict("[ReceivedTime] >= '" +lastMinuteDateTime.strftime('%m/%d/%Y %H:%M %p')+"'")

print "Current time: "+date_time.strftime('%m/%d/%Y %H:%M %p')
print "Messages from the past hour:"

for message in lastHourMessages:
print message.subject
print message.ReceivedTime

print "Messages from the past minute:"

for message in lastMinuteMessages:
print message.subject
print message.ReceivedTime

# GetFirst/GetNext will also work, since the restricted message list is just a shortened version of your full inbox.
print "Using GetFirst/GetNext"
message = lastHourMessages.GetFirst()
while message:
print message.subject
print message.ReceivedTime
message = lastHourMessages.GetNext()

您似乎让它每 20 秒运行一次,因此大概您可以以不同的时间间隔运行它。如果您不能定期可靠地运行它(然后将在 timedelta 中指定,例如 hours=1),您可以保存最近检查的电子邮件的 ReceivedTime,并使用它来限制您的搜索。 (在这种情况下,保存的 ReceivedTime 将替换 lastHourDateTime,并且 Restrict 将检索在最后一封检查后发送的每封电子邮件。)

希望对您有所帮助!

关于python - 如何查看一段时间内收到的所有邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45425251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com