nut_flush
nut_flush

Reputation: 11

How to use Python COM to filter for emails from certain senders?

I'm trying to automate getting attachments from certain emails and the documentation for win32com.client is horrendous.

So far I've got the following:

import win32com.client as win32
import os

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.Folders["Payments"].Folders["Inbox"]

messages = inbox.Items


for i in range(10):

    message = messages.GetNext()
    print(message.Sender)
    print(message.Subject)
    print(message.ReceivedTime)
    attachment = message.attachments

    for j in attachment:
        j.SaveAsFile(os.getcwd() + "\\" + j.FileName)

However, I only want to get attachments from say "[email protected]" which I can't figure out to do.

Is there a way to only get the emails and their attachments from certain senders (bonus if I can also filter for the email title)?

Upvotes: 0

Views: 5558

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Use a restriction like filteredItems = Inbox.Items.Restrict("[SenderEmailAddress] = '[email protected]' ") The documentation is at https://learn.microsoft.com/en-us/office/vba/api/outlook.items.restrict

Upvotes: 2

Related Questions