-
-
Save allanpedroni/ccf22fae4b4ea18a48ec615bd7f46bf1 to your computer and use it in GitHub Desktop.
Extract Email from Outlook with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32com.client | |
items = [] | |
def encodeit(s): | |
if isinstance(s, str): | |
return unicode(s, 'utf-8') | |
else: | |
return s | |
def extract(): | |
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") | |
inbox = outlook.GetDefaultFolder(6) # "6" refers to the inbox | |
messages = inbox.Items | |
message = messages.GetFirst() | |
while message: | |
try: | |
d = dict() | |
d['Subject'] = encodeit(getattr(message, 'Subject', '<UNKNOWN>')) | |
d['SentOn'] = encodeit(getattr(message, 'SentOn', '<UNKNOWN>')) | |
d['EntryID'] = encodeit(getattr(message, 'EntryID', '<UNKNOWN>')) | |
d['Sender'] = encodeit(getattr(message, 'Sender', '<UNKNOWN>')) | |
d['Size'] = encodeit(getattr(message, 'Size', '<UNKNOWN>')) | |
d['Body'] = encodeit(getattr(message, 'Body', '<UNKNOWN>')) | |
items.append(d) | |
except Exception as inst: | |
print "Error processing mail" | |
message = messages.GetNext() | |
def showMessage(): | |
items.sort(key=lambda tup: tup['SentOn']) | |
for i in items: | |
print i["SentOn"], i["Subject"] | |
extract() | |
showMessage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment