当您可以简单地从 python 中完成所有操作时,为什么要在 Outlook 中创建一个在收到电子邮件时运行脚本的规则。
使用 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()