【问题标题】:How to pass a parameter in an Outlook rule to run a Python script?如何在 Outlook 规则中传递参数以运行 Python 脚本?
【发布时间】:2012-04-26 18:14:22
【问题描述】:

我创建了一个用于关闭我的计算机的 shutdown.py 脚本。
我在 Microsoft Outlook 中有一条工作规则,当我收到主题中包含 %BLAHBLAHBLAH% 的电子邮件时,它会执行我的 Python 脚本。

是否可以在执行之前将电子邮件的主题行传递到 Python 脚本中?
基本上,我希望主题行中有一个关键字来执行某个脚本,但也能够将参数“传递”到电子邮件的主题行到 Python 脚本。
例如,如果我发送 %shutdown30% 我的 python 脚本将解析字符串 %shutdown30% 并使用 30 作为参数在 30 分钟内关闭计算机。

【问题讨论】:

  • 我不知道 Outlook 能做什么,但 python 当然可以接受命令行参数。查看 sys.argv docs.python.org/library/sys.html#sys.argv 如果您可以让 Outlook 执行类似 python shutdown.py %subject% 之类的操作,那么 sys.argv 就可以了。
  • 谢谢,但这并不是我真正想要的。 Outlook 允许您创建规则。如果您收到主题为“BLAH”的电子邮件,您可以创建一条规则,说明运行此应用程序。所以收到的带有指定主题的邮件会触发python脚本,问题是有没有办法让python看到
  • 收到主题。我猜没有,但也许有人知道我不知道的事情。不过感谢您的回复!

标签: python outlook rules


【解决方案1】:

为什么要在 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() 

【讨论】:

  • 很棒的资源!我不知道 Outlook 可以这样使用。这带来了无数的机会。
  • 是的,这太酷了。一个人将如何将其作为背景,也许是作为一项服务或其他东西?所以它不占用命令窗口。
  • 这看起来很酷,但是谁能解释一下 DispatchWithEvents 的作用(以及它与 Dispatch 的区别),以及上述脚本的每个部分是如何工作的?我在 Google 上几乎没有找到关于 DispatchWithEvents 的教程,所以任何帮助都会很棒!
  • 优秀。 +1以获得清晰的解释。谢谢@YusuMishi
  • @guialmachado 您需要将 BLAHBLAH 括在两个“%”之间。喜欢这个“%BLAHBLAH%”。 BLAHBLA 可以是任何命令或字。最重要的是用“%”括起来。
猜你喜欢
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多