2008-03-20

Export emails from Outlook

"""
Exports email address from MS Outlook from recent eemails
"""

from win32com.client import Dispatch, constants
import os.path

class Exporter(object):

app = None
emails = []

def __init__ (self):

self.app = Dispatch("Outlook.Application")
if self.app == None:
raise Exception, "Unable to create an Outlook application object"

def export_emails(self, foldername, outfile="email.csv", maxcount=1000):

emails = []
ns = self.app.GetNamespace("MAPI")
folders = ns.Folders.Item(1).Folders #Usually the users folders
folder = folders.Item(foldername)
cnt = len(folder.Items)
if cnt > maxcount:
cnt = maxcount
for i in range(1, cnt + 1):
item = folder.Items.Item(i)
print "Processing item %d" % i
self.export_from_field(item, "To")
self.export_from_field(item, "CC")

self.emails.sort()
emailfile = open(outfile, 'w')
for email in self.emails:
emailfile.write(email + "\n")
emailfile.close()

def export_from_field(self, item, fieldname, maxcount = 10):
if not hasattr(item, fieldname):
return
ms = getattr(item, fieldname).split(';')
if len(ms) > maxcount:
return
for m in ms:
parts = m.split(',')
if len(parts) <> 2:
continue
email = "%s_%s@intuit.com" % (parts[1].strip(), parts[0].strip())
if not email in self.emails:
self.emails.append(email)

def __del__ (self):

self.app = None
self.emails = None

def main ():
export = Exporter()
export.export_emails("InBox", "emails.csv", 1000)

if __name__ == "__main__":
main();

3 comments:

Anonymous said...

Hi! I've been following your site for a while now and finally got the courage to go ahead and give you a shout out from Porter Texas! Just wanted to mention keep up the great job!

Here is my blog post - golf clubs orlando weddings

Anonymous said...

Your method of explaining everything in this piece of writing is truly
pleasant, all be able to without difficulty be aware of it, Thanks a lot.


Also visit my website: nike golf shoes cheap

Anonymous said...

Quality posts is the secret to attract the visitors to go to see the website, that's what this web page is providing.

Visit my page Championgolfgifts.com