Python Forum

Full Version: Outlook mail watcher
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

Greeting to all, i am new learner, i just want to write app, which monitors outlooks applications and parse the newly received mail, extract mail information.
I have found some reference code for that task which i posted below.
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
            body = mail.Body
            print(subject)
            print(body)
            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() 
I understand the code some how. But its is very useful if you explained in detailed manner.

The problem i am facing is, the body of the mail is not fully received in the code "body = mail.Body".
I want to extract the body of mail fully even though it is a large chain mail.
More over i want to read tables also if its in mail body.
Please provide your suggestion and ideas to solve this issue.
Thanks for spending your time for reading my post.
Is there a reason you're interfacing with outlook, instead of just using imap to query the server directly?