I get a lot of email at work. Mostly by robots that are triggering on some event or another. But after a few years of more and more distribution lists having me on it, the amount of email I get in a day is actually unmanageable. I’m talking thousands. The vast majority can be safely ignored, such as “Job ABC completely successfully”.
So I’ve gotten pretty aggressive with Outlook rules in order to filter my incoming email into different folders. But the rules have always felt limited to me. For example, I cannot say “If the subject contains this or this, do that“. I’m limited to a logical and.
Looking into whether or not you can use Python in your Outlook rules, I came across an example where you can actively monitor your Inbox and have access to the messages that come in, all from Python.
C:meDevelopmentpythonoutlook>outlook-monitor.py Subj: Test Email Body: Hey! Test -Matthew Urch | Programmer Extraordinaire ========
This opens up a whole world of email automation.
The basic script I made that achieves the above is this:
import win32com.client import pythoncom class Handler_Class(object): def OnNewMailEx(self, receivedItemsIDs): for ID in receivedItemsIDs.split(","): # Microsoft.Office.Interop.Outlook _MailItem properties: # https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem_properties.aspx mailItem = outlook.Session.GetItemFromID(ID) print "Subj: " + mailItem.Subject print "Body: " + mailItem.Body.encode( 'ascii', 'ignore' ) print "========" outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class) pythoncom.PumpMessages()
And then from here, you can do whatever you like with your email messages that are coming in.