here is some usage ...
outlook = OutlookLib() messages = outlook.get_messages('you@yourcompany.com') for msg in messages: print msg.Subject print msg.Body
and here is the class ...
import win32com.client
class OutlookLib:
def __init__(self, settings={}):
self.settings = settings
def get_messages(self, user, folder="Inbox", match_field="all", match="all"):
outlook = win32com.client.Dispatch("Outlook.Application")
myfolder = outlook.GetNamespace("MAPI").Folders[user]
inbox = myfolder.Folders[folder] # Inbox
if match_field == "all" and match =="all":
return inbox.Items
else:
messages = []
for msg in inbox.Items:
try:
if match_field == "Sender":
if msg.SenderName.find(match) >= 0:
messages.append(msg)
elif match_field == "Subject":
if msg.Subject.find(match) >= 0:
messages.append(msg)
elif match_field == "Body":
if msg.Body.find(match) >= 0:
messages.append(msg)
#print msg.To
#msg.Attachments
# a = item.Attachments.Item(i)
# a.FileName
except:
pass
return messages
def get_body(self, msg):
return msg.Body
def get_subject(self, msg):
return msg.Subject
def get_sender(self, msg):
return msg.SenderName
def get_recipient(self, msg):
return msg.To
def get_attachments(self, msg):
return msg.Attachments
Works like a butter. Building a tool to manage the mails. Will help a lot. Thanks buddy..!! :)
ReplyDeleteWorks really well but I can't figure out how to work with attachments. I am expecting an email with a file attached that contains data. The data from that file needs to be read into a dataframe.
ReplyDeleteI am unable to install win32com.client. Can you please help ? I am using Python 3.3.5 on Windows 7 32bit OS
ReplyDeleteuse anaconda
Deleteinstall pywin32,it will gives you win32com.client
DeleteSeriously. Thanks so much Sir !! Definitely works like a butter
ReplyDeletehi i do not have permission to install pywin32. Is there any work around that.
ReplyDelete