Microsoft Outlook创建规则运行应用程序/脚本Python [英] Microsoft Outlook Create Rule Run Application/Script Python

查看:498
本文介绍了Microsoft Outlook创建规则运行应用程序/脚本Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个shutdown.py脚本,该脚本在执行时会关闭我的计算机.我还在Microsoft Outlook中创建了一条规则,当我收到主题中包含%BLAHBLAHBLAH%的电子邮件时,该规则将执行我的Python脚本.我已经对其进行了测试,并且可以完美地工作.但是,我对所有人的问题是:是否可以在执行电子邮件之前将电子邮件的主题行传递到Python脚本中?基本上,我希望在主题行中有一个关键字,该关键字将执行某个脚本,但也能够将参数传递"到Python脚本随后将使用的电子邮件主题行中.例如,如果我发送%shutdown30%,我的python脚本将能够解析字符串%shutdown30%,并使用30作为参数在30分钟内关闭计算机.

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

推荐答案

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

这篇关于Microsoft Outlook创建规则运行应用程序/脚本Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆