2

I have gotten some of the features I want but need help with 2 others.

  1. I would like to flag the message "Mark as Done" (it's one of the Flag statuses). I have not found how to do this.

  2. If I wanted to do this same thing for 4 other emails how would I do it, with 4 other save paths?

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 == '[email protected]' 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 == '[email protected]' and m.Unread == True:
            path = 'C:\\User\\Path\\Data\\Jane'

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

1 Answer 1

1

You have to save it message.Save(), Example

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()

For multiple emails & path use dictionary, Example

emails_with_path = {
    "[email protected]": "path_one",
    "[email protected]": "path_two",
    "[email protected]": "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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.