Python Forum

Full Version: Search Outlook Inbox for set of values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So im being tasked with capturing status of key things within multiple projects.. The only bad thing is that not everyone follows the email standard for providing their updates.

I can clearly use the search feature in outlook but that means i would have to search one at a time and rather see if i can make something that will search my entire inbox for a set of values.. and also check if the "TO" or "CC" contains 1 specific email address.

I already know how to connect from python to outlook, im using for processing attachments, but not clear how i can search for values and output a list of emails that contain those values and email address.

Can anyone suggestion methods or sources where i can look at how to accomplish, im working on a way i think may work, since i already have working outlook code, but not ready to test yet or post here.. but will once i get something that runs and can ask for specific help.
Ok so here is what i have so far and i can get a count of messages from my inbox for the last 10 days..
But need to update the For Loop condition to only return records that contain any of the values in my list

import win32com.client
from datetime import datetime, timedelta
from NewStoreEmailVariables import impStores  # THIS IS A USER LISTING STORED IN THE VARIABLE.PY FILE

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
  
inbox = mapi.GetDefaultFolder(6)

messages = inbox.Items

# THIS IS USED TO GRAB THE MESSAGE DATE SO IT CAN PROCESS ONLY THE LATEST EMAILS (EXAMPLE BELOW IS LAST 10 DAYS)
received_dt = datetime.now() - timedelta(days=10)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')

messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")

print(messages.count)

for message in messages:
    if message.Class == 43:
        if (str(message.sender) in impStores):
            print(str(message.sender)+ " = " + str(message.subject) + " = " + str(message.ReceivedTime))
Separate file with the following:

impStores = [    
    '353',
    '336',
    '349',
    '345'
    ]