17

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.

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.

Community
  • 1
  • 1
papezjustin
  • 2,223
  • 6
  • 24
  • 31
  • I don't know what Outlook can do but python can certainly take command line arguments. Check out sys.argv http://docs.python.org/library/sys.html#sys.argv If you can make outlook do something like python shutdown.py %subject% then sys.argv would work. – joshcartme Apr 14 '12 at 22:12
  • Thanks, but that's not really what I am looking for. Outlook allows you to create rules. You can create a rule that says run this application if you receive an email with "BLAH" in the subject. So the received email with the specified subject triggers the python script, the question is is there any way to give python sight of the – papezjustin Apr 14 '12 at 22:54
  • received subject. I am guessing no, but maybe someone is aware of something I am not. Thanks for your response though! – papezjustin Apr 14 '12 at 22:54

1 Answers1

46

Why creating a rule in outlook that runs a script if an email is received, when you can simply do it all from python.

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() 
YusuMishi
  • 2,317
  • 1
  • 18
  • 8
  • 1
    Fantastic resource! I had no idea that Outlook could be used this way. This opens up countless opportunities. – Stuart Woodward May 23 '12 at 01:18
  • 3
    Yeah, this is amazingly cool. How would one go about back-grounding this, maybe as a service or something? So it's not occupying a command window. – Sushisource Aug 30 '13 at 21:25
  • 1
    This seems very cool, but could anyone please explain more about what DispatchWithEvents does (and how it differs from Dispatch), and how each piece of the above script works? I found little on Google for DispatchWithEvents in the way of tutorials, so any help would be great! – Ward W Jun 09 '16 at 20:29
  • Excellent. +1 for the clear explanation. Thanks @YusuMishi – Swadhikar Aug 22 '17 at 09:00
  • I am trying to use this code, but i am not able to change the subject to my needs. I am trying with BLAHBLAH on e-mail subject and it doesnt run the code after the regex search – guialmachado May 06 '20 at 18:21
  • 1
    @guialmachado you need to enclose BLAHBLAH in between two "%". like This "%BLAHBLAH%". BLAHBLA can be any command or word. the most important is to have it enclose by "%". – YusuMishi May 06 '20 at 20:47
  • I found what was wrong. i changed ( for [ and it worked. now i am using `%[BLAHBLAH]%` – guialmachado May 06 '20 at 21:15