17

コンピューターをシャットダウンするshutdown.pyスクリプトを作成しました。
件名に%BLAHBLAHBLAH%が含まれる電子メールを受信すると、Pythonスクリプトを実行するMicrosoftOutlookの作業ルールがあります。

メールの件名を実行する前にPythonスクリプトに渡すことはできますか?
基本的に、件名のキーワードで特定のスクリプトを実行するだけでなく、パラメーターを電子メールの件名にPythonスクリプトに「渡す」こともできます。
たとえば、%shutdown30%を送信すると、Pythonスクリプトは文字列%shutdown30%を解析し、パラメーターとして30を使用して、30分でコンピューターをシャットダウンします。

4

1 に答える 1

46

すべてを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() 
于 2012-05-10T17:17:39.450 に答える