Microsoft Outlook Create Rule Run Application/Script Python

前端 未结 1 2019
慢半拍i
慢半拍i 2020-12-07 14:59

I have created a shutdown.py script that shuts down my computer when executed. I have also created a rule in Microsoft Outlook that executes my Python script when I receive

相关标签:
1条回答
  • 2020-12-07 15:41

    Why creating a rule in outlook that runs a script if an email is received, when you can simply do it all from python.

    Using Python to monitor outlook for all incoming emails and then execute some code if an email, with %BLAHBLAH% in the subject, is received is possible. Here is an example:

    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:
                    # Taking all the "BLAHBLAH" which is enclosed by two "%". 
                    command = re.search(r"%(.*?)%", subject).group(1)
    
                    print command # Or whatever code you wish to execute.
                except:
                    pass
    
    
    outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)
    
    #and then an infinit loop that waits from events.
    pythoncom.PumpMessages() 
    
    0 讨论(0)
提交回复
热议问题