簡體   English   中英

Python Outlook 收件箱保存附件 win32com.client

[英]Python Outlook inbox saving attachments win32com.client

我已經獲得了一些我想要的功能,但需要另外 2 個功能的幫助。

  1. 我想標記消息“標記為完成”(這是標記狀態之一)。 我還沒有找到如何做到這一點。

  2. 如果我想為其他 4 封電子郵件做同樣的事情,我會怎么做,還有 4 個其他保存路徑?

import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inboxfolder = Outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
inbox = inboxfolder.Items
message = inbox.GetFirst()
subject = message.Subject
sender = message.SenderEmailAddress

for m in inbox:
    if m.Class == 43: # this is to make sure it is an email item and not something else. 
        if m.SenderEmailAddress == 'John@email.com' and m.Unread == True:
            path = 'C:\\User\\Path\\Data\\John'
            print ('Subject as: ' and message)
            for attached in message.Attachments:
                attached.SaveASFile(os.path.join(path,attached.FileName)) #Saves attachment to current folder
                print (attached)
            message.Unread = False
            print (message.FlagStatus)
            message.FlagStatus = 1  # This is to "mark as Done" but it doesn't work
            message = inbox.GetNext()
        elif m.SenderEmailAddress == 'Jane@email.com' and m.Unread == True:
            path = 'C:\\User\\Path\\Data\\Jane'

            # ... How would you add 4 more? 
            message = inbox.GetNext()
        else:
            message = inbox.GetNext()

您必須將其保存message.Save() ,示例

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

for Item in Inbox.Items:
    if Item.Class == 43:
        Item.FlagStatus = 1
        Item.Save()

對於多個電子郵件和路徑使用字典,示例

emails_with_path = {
    "email@one.com": "path_one",
    "email@two.com": "path_two",
    "email@three.com": "path_three"
}

for m in inbox:
    if m.Class == 43:
        for email, path in emails_with_path.items():
            if m.SenderEmailAddress == email and m.UnRead:
                print(email)
                print(path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM