-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlook_mail.py
106 lines (86 loc) · 3.25 KB
/
outlook_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import win32com.client
import win32com
import os
import sys
def read_accounts(outlook):
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts
for account in accounts:
print(account.DisplayName)
inbox = outlook.Folders(account.DeliveryStore.Displayname)
yield inbox
def read_folders(inbox):
for folder in inbox.folders:
print(folder.name)
yield folder
def get_email(recipient):
try:
props = recipient.PropertyAccessor
emailaddress = props.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
except Exception as ex:
emailaddress = ex
return emailaddress
def process_folder(folder):
messages = folder.Items
for message in messages:
item = {}
try:
item["subject"] = message.Subject
item["body"] = message.Body
try:
item["sender"] = message.Sender
item["senderaddress"] = message.SenderEmailAddress
item["sendertype"] = message.SenderEmailType
item["sendername"] = message.SenderName
if message.SenderEmailType == "EX":
item["senderemail"] = get_email(message.Sender)
else:
item["senderemail"] = message.SenderEmailAddress
except:
pass
try:
item["conversationindex"] = message.ConversationIndex
item["conversationid"] = message.ConversationID
item["conversationtopic"] = message.ConversationTopic
except:
pass
item["actors"] = []
for r in message.Recipients:
actor = {}
try:
actor["address"] = r.Address
actor["id"] = r.EntryID
actor["name"] = r.Name
actor["type"] = r.Type
actor['email'] = get_email(r)
except Exception as ex:
actor["error"] = ex
item["actors"].append(actor)
try:
item['sent'] = message.SentOn
item["to"] = message.To
item["cc"] = message.CC
item["bcc"] = message.BCC
except:
# not all objects have the to, cc and bcc attribs
pass
except Exception as ex:
item["error"] = ex
yield item
def print_item(item, item_keys):
print("===")
for key in item_keys:
if key in item:
print("\t{}:{}".format(key, item[key]))
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder_names = ['Inbox']
item_keys = ['subject', 'sent', 'sendername', 'senderemail', 'sendertype', 'conversationtopic', 'conversationindex', 'conversationid', 'to', 'cc', 'bcc', 'actors', 'error', 'body']
item_count = 0
for inbox in read_accounts(outlook):
for folder in read_folders(inbox):
if folder.Name in folder_names:
for item in process_folder(folder):
item_count += 1
print_item(item, item_keys)
if item_count % 1000 == 0:
break
print("found {} items".format(item_count))