views:

397

answers:

3

I want to intercept and transform some automated emails into a more readable format. I believe this is possible using VBA, but I would prefer to manipulate the text with Python. Can I create an ironpython client-side script to pre-process certain emails?

EDIT: I believe this can be done with outlook rules. In Outlook 2007, you can do: Tools->Rules -> New Rule

"check messages when they arrive"

next

[filter which emails to process]

next

"run a script"

In the "run a script" it allows you to use a VBA script.

+2  A: 

I can only offer you a pointer. Years ago, I used a Bayesian spam filter with Outlook. It was written in Python, and provided an Outlook plug-in that would filter incoming mail. The name of the software was SpamBayes, and the project is still online. As it is open source, you will probably find all necessary information how to plug a mail filter into Outlook. This should give you enough background to add code that will actually be able to transform mail content. My understanding was it was written in vanilla Python (CPython), but if you are more comfortable with IronPython, it shouldn't be hard to translate. Give it a go.

ThomasH
Thanks! I've never worked with ironpython, I just assumed that was how you'd need to interact with outlook. I normally use CPython.
Ross Rogers
@Ross Meanwhile I've read that the venerable Mark Hammond has been on this project, particularly for the plug-in, so there probably went quite a bit of win32 coolness into it :-) . Mark is responsible for much of the good integration of (C)Python with Windows.
ThomasH
+2  A: 

You can reference the outlook object model here: http://msdn.microsoft.com/en-us/library/ms268893.aspx

Connect to outlook through COM, you'll need pywin32.

There's no python reference that I know of, but you can reference the sample scripts and 'translate' to python. It's difficult at first, but once you understand the objects and their usage in python it's not hard.

Looks like you want to look at:

How to: Perform Actions When an E-Mail Message Is Received

monkut
A: 

This is a work in progress, but I've figured out part of the answer with the help of the other posts. Here are the instructions for re-writing specified emails by running a script. I'm using Outlook 2007.

  1. Download and install pywin32

  2. Download and install ExchangeCdo.exe

  3. Put this code in a file and run it from cmd:

import os, sys, re
import win32com.client
session = win32com.client.gencache.EnsureDispatch("MAPI.session")
win32com.client.gencache.EnsureDispatch("Outlook.Application")
outlook = win32com.client.Dispatch("Outlook.Application")
#print '\n'.join(dir(outlook))
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(win32com.client.constants.olFolderInbox)
items = inbox.Items
#items.Restrict("[Unread] = true")
#print '\n'.join(dir(items))
while True:
    item = items.GetNext()
    if item == None: break
    #print '\n'.join(dir(item))
    if re.compile(r'crazy email').search(item.Subject):
        print item.Subject
        print item.Body
        # works VVVV
        item.Body = 'whoya!'
        item.Save()
        break
Ross Rogers
+1 Nice work, Ross.
ThomasH