Microsoft Outlook Create Rule Run Application/Script Python

孤人 提交于 2019-11-27 09:38:04

问题


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 an email that has %BLAHBLAHBLAH% in the subject. I have tested it out and it works flawlessly; however, my question for you all is: is it possible to pass the email's subject line into the Python script before executing it? Basically, I want to have a keyword in the subject line that will execute a certain script but also be able to "pass" parameters into the email's subject line that the Python script will then use. For example if I sent %shutdown30% my python script would be able to parse the string %shutdown30% and use the 30 as a parameter to shutdown the computer in 30 minutes.

Thanks in advance for any advice/comments/suggestions/answers :)


回答1:


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() 


来源:https://stackoverflow.com/questions/10156795/microsoft-outlook-create-rule-run-application-script-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!