gpt4 book ai didi

python - 使用python将今天收到的电子邮件中的附件复制到文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:21 24 4
gpt4 key购买 nike

我有一个脚本可以将附件从 Outlook 复制到笔记本电脑上的文件夹中。到目前为止一切顺利,如果我只有一封电子邮件,其中包含已定义的主题和附件,一切正常。今天我意识到,当我的收件箱中有一封新旧电子邮件具有相同的主题和附件名称时,会出现一个问题 - 看起来它随机使用旧电子邮件或新电子邮件。

问题:有没有办法告诉脚本要么总是接受最新的邮件,要么接受今天收到的邮件?我尝试使用 GetLast() 和 GetFirst(),这是我在 stackoverlow 中找到的,但不确定将它添加到哪里(我的尝试导致错误)。有人有想法吗?

from win32com.client import Dispatch
import datetime as date

outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()

sub_today = 'Email Subject'
att_today = 'Attachment.zip'

for msg in all_inbox:
if msg.Subject == sub_today:
break

for att in msg.Attachments:
if att.FileName == att_today:
break


att.SaveAsFile(r'C:\path\to\my\folder\Attachment.zip')

编辑(解决方案):

import win32com.client
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder("6")

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:subject" +
chr(34) + " Like 'ATTACHMENTNAMEHERE' AND " +
chr(34) + "urn:schemas:httpmail:hasattachment" +
chr(34) + "=1")


Items = Inbox.Items.Restrict(Filter)
Items.Sort('[ReceivedTime]', False)
Item = Items.GetLast()

for attachment in Item.Attachments:
print(attachment.FileName)
if attachment.FileName == "ATTACHMENT.zip":
attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.zip")

最佳答案

下面的怎么样...


import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:subject" +
chr(34) + " Like 'Email Subject' AND " +
chr(34) + "urn:schemas:httpmail:hasattachment" +
chr(34) + "=1")

Items = Inbox.Items.Restrict(Filter)
Items.Sort('[ReceivedTime]', False)
Item = Items.GetLast()

for attachment in Item.Attachments:
print(attachment.FileName)
if attachment.FileName == "Attachment.zip":
attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.zip")

Items.GetLast method (Outlook)

Items.Restrict method (Outlook)

关于python - 使用python将今天收到的电子邮件中的附件复制到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57924604/

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