如何在 Outlook 规则中传递参数以运行 Python 脚本? [英] How to pass a parameter in an Outlook rule to run a Python script?
问题描述
我创建了一个shutdown.py脚本来关闭我的电脑.
我在 Microsoft Outlook 中有一个工作规则,当我收到一封主题为 %BLAHBLAHBLAH% 的电子邮件时,它会执行我的 Python 脚本.
I created a shutdown.py script that shuts down my computer.
I have a working rule in Microsoft Outlook that executes my Python script when I receive an email that has %BLAHBLAHBLAH% in the subject.
是否可以在执行之前将电子邮件的主题行传递到 Python 脚本中?
基本上,我希望主题行中的关键字执行某个脚本,但也能够通过"将参数添加到 Python 脚本的电子邮件主题行中.
例如,如果我发送 %shutdown30%,我的 Python 脚本将解析字符串 %shutdown30% 并使用 30 作为参数在 30 分钟内关闭计算机.
Is it possible to pass the email's subject line into the Python script before executing it?
Basically, I want a keyword in the subject line to execute a certain script but also be able to "pass" parameters into the email's subject line to the Python script.
For example if I send %shutdown30% my python script would parse the string %shutdown30% and use the 30 as a parameter to shutdown the computer in 30 minutes.
推荐答案
为什么要在 Outlook 中创建一个规则来在收到电子邮件时运行脚本,而您可以通过 Python 轻松完成所有操作.
Why creating a rule in outlook that runs a script if an email is received, when you can simply do it all from python.
使用 Python 监控所有传入电子邮件的前景,然后在收到主题为 %BLAHBLAH% 的电子邮件时执行一些代码.下面是一个例子:
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()
这篇关于如何在 Outlook 规则中传递参数以运行 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!