-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgethab.py
47 lines (40 loc) · 1.18 KB
/
gethab.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
from exchangelib import Credentials, Account, DELEGATE
import json
USERNAME = "WINDOMAIN\\yourusername"
PASSWORD = "yourpassword"
SMTP = "[email protected]"
TOP = "toplevelname"
credentials = Credentials(username=USERNAME, password=PASSWORD)
my_account = Account(
primary_smtp_address=SMTP,
credentials=credentials,
autodiscover=True,
access_type=DELEGATE,
)
def build_hierarchy(TOP):
hierarchy = []
for i in my_account.protocol.expand_dl(TOP):
if i.mailbox_type == "PublicDL":
hierarchy.append(
{
"name": i.name,
"email_address": i.email_address,
"mailbox_type": i.mailbox_type,
"children": build_hierarchy(i.email_address),
}
)
else:
hierarchy.append(
{
"name": i.name,
"email_address": i.email_address,
"mailbox_type": i.mailbox_type,
}
)
return hierarchy
hierarchy = build_hierarchy(TOP)
with open("output.json", "w", encoding="utf-8") as f:
json.dump(
hierarchy,
f,
)