Python read my outlook email mailbox and parse messages

user1650464 picture user1650464 · Sep 6, 2012 · Viewed 32.6k times · Source

Possible Duplicate:
Reading e-mails from Outlook with Python through MAPI

I am completely new to Python and have been given the task to write a program that connects to my Microsoft Outlook mailbox, goes through all the emails and if the subject has a certain word, then the details of the email time and subject will be saved in variables, as well as the email message body will be parsed and relevant information will be stored in variables. Then this information will be stored in an external server/database. It also needs to be able to monitor any new emails that comes to my mailbox and repeat the drill of checking the subject line and taking appropriate action.

I have written exactly the same kind of program in C# earlier using the Interop library, but now need to do this in Python. I can figure out the nitty-gritty details by readin gthe module documentations later on, but from a high level perspective what modules should I use. I have been doing my research and some modules that have been mentioned include email, procmail and imaplib, but what do the Python veterans here recommend for the kind of project I am overtaking?

Thanks in advance for any help you might be able to provide!

Answer

Carlos Henrique Cano picture Carlos Henrique Cano · Sep 9, 2012

At one company I worked we have a mailbox for suggestion with websites that had 'adult' material and one mailbox for spam mail that should be blocked. Once I began working I was in 'charge' of this 'gracious' jobs. Checking it there was something like 2000 unread mails to block and 4000 spam mails to block too. Of course that is a function to be automatized and I looked for a good solution for me. What I did:

[1] Used python IMAP to connect to Exchange server [2] Used beatifulsoup (python) to parse the href values inside the email [3] After that send a email 'thanking' the user for its collaboration (very important)

Three days after my boss thanked me for the great effort I was doing answering all the e-mails and that we got compliments. Because NOW we are answering back the customers. (not me the script)

Ok. now lets do a plan

  1. Check the imap python module [1], and after take one tutorial using ssl imap4 [4]
  2. Decide What is best for YOUR problem? Download the emails (pop3) or search and browse it at server (IMAP).
  3. CHECK if you can connect using the protocols IMAP4 or POP3 Before, exchange is buggy in this part please check this bug report too [3]
  4. Ok, you are sure you can connect using IMAP4 or POP3, now fetch one message and parse it with beatiful soup or lxml. (my case I looked for href and 'mailto:')
  5. Do a nice message using the field 'from:' the email making it personal
  6. PROFIT

[1] google it imap python

[2] google it BeautifulSoup python

[3] http://support.microsoft.com/kb/296387

[4] http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/

Sorry but I had to give the google urls because of my low score.

I hope this answer give you some good pointers to your solution. Of course you can make it more hax0r using lxml, sending the data to a DB. But after you connect and start manipulating you can do anything :)