Last active
July 5, 2018 14:05
-
-
Save burdenless/fd2c92e468a3d07f5c37 to your computer and use it in GitHub Desktop.
Outlook Listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32com.client | |
import pythoncom | |
import re | |
class Handler_Class(object): | |
def OnNewMailEx(self, receivedItemsIDs): | |
# RecrivedItemIDs is a collection of mail IDs separated by a ",". | |
# You know, sometimes more than 1 mail is received at the same moment. | |
for ID in receivedItemsIDs.split(","): | |
mail = outlook.Session.GetItemFromID(ID) | |
subject = mail.Subject | |
try: | |
# Parse the subject line | |
command = re.search(r"whatevs", subject).group(1) | |
print command # Or whatever code you wish to execute. | |
except: | |
pass | |
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class) | |
# an infinite loop that waits for event | |
pythoncom.PumpMessages() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/41057336/win32com-client-dispatchwithevents-for-multiple-objects-in-the-same-thread