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.

3
  • I don't know what Outlook can do but python can certainly take command line arguments. Check out sys.argv 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. Commented Apr 14, 2012 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 Commented Apr 14, 2012 at 22:54
  • received subject. I am guessing no, but maybe someone is aware of something I am not. Thanks for your response though! Commented Apr 14, 2012 at 22:54

1 Answer 1

47

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() 
Sign up to request clarification or add additional context in comments.

7 Comments

Fantastic resource! I had no idea that Outlook could be used this way. This opens up countless opportunities.
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.
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!
Excellent. +1 for the clear explanation. Thanks @YusuMishi
@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 "%".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.