Skip to content

Instantly share code, notes, and snippets.

@burdenless
Last active July 5, 2018 14:05
Show Gist options
  • Save burdenless/fd2c92e468a3d07f5c37 to your computer and use it in GitHub Desktop.
Save burdenless/fd2c92e468a3d07f5c37 to your computer and use it in GitHub Desktop.
Outlook Listener
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()
@brainscience
Copy link

This is really a useful tool. I'd like to poll a shared inbox like this. I can reach the shared inbox and gather up the email there but I'd like to be notified when a new email arrives. Any ideas?

@rockyfjord
Copy link

I was hoping to find an example where someone is tracking ItemAdd event. OnNewMailEx only works for the default inbox. It would be great to know how to do this with a shared inbox! Unfortunately most examples I find are in VBA and I've only a year of experience with Python.

recipient = outlook.CreateRecipient("[email protected]")
shared = outlook.GetSharedDefaultFolder(recipient, 6)

@jdubp
Copy link

jdubp commented Jun 3, 2017

Did either of you find any examples using ItemAdd? I'm trying to do the same thing (monitor a non-default inbox for new messages) using Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment