0

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 "payments@email.com" 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)?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
nut_flush
  • 11
  • 1
  • 2

1 Answers1

2

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78