Jus*_*pez 16 python outlook rule
我创建了一个shutdown.py脚本,在执行时关闭我的计算机.我也创建Microsoft Outlook中的规则,当我收到的受试者%BLAHBLAHBLAH%一封电子邮件,执行我的Python脚本.我测试了它,它完美无瑕; 但是,我的问题是:是否可以在执行之前将电子邮件的主题行传递到Python脚本中?基本上,我想在主题行中有一个关键字,它将执行某个脚本,但也能够将参数"传递"到Python脚本随后将使用的电子邮件主题行中.例如,如果我发送%shutdown30%,我的python脚本将能够解析字符串%shutdown30%并使用30作为参数在30分钟内关闭计算机.
在此先感谢任何建议/意见/建议/答案:)
Yus*_*shi 40
为什么在Outlook中创建一个规则来运行脚本,如果收到一封电子邮件,那么你可以从python中完成所有操作.
使用Python监视所有传入电子邮件的Outlook,然后在收到主题中包含%BLAHBLAH%的电子邮件时执行某些代码.这是一个例子:
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()
Run Code Online (Sandbox Code Playgroud)