-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieve_messages_all_emails.py
More file actions
59 lines (47 loc) · 2.04 KB
/
Copy pathretrieve_messages_all_emails.py
File metadata and controls
59 lines (47 loc) · 2.04 KB
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
import os
import httpx
from dotenv import load_dotenv
from ms_graph import get_access_token,MS_GRAPH_BASE_URL
from tqdm import tqdm
def main():
load_dotenv()
APPLICATION_ID = os.getenv('APPLICATION_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
SCOPES = ['User.Read','Mail.ReadWrite']
endpoint = f'{MS_GRAPH_BASE_URL}/me/messages'
try:
access_token = get_access_token(application_id=APPLICATION_ID,client_secret=CLIENT_SECRET,scopes=SCOPES)
headers = {'Authorization':'Bearer '+access_token}
for i in range(0,4,2):
params = {
'$top':i+2,
'$select':'*',
'$skip':i,
'$orderBy':'receivedDateTime desc'
}
response = httpx.get(endpoint,headers=headers,params=params)
if response.status_code != 200:
raise Exception(f'Failed to retrieve emails: {response.text}')
json_response = response.json()
for mail_message in tqdm(json_response.get('value',[])):
print(mail_message)
print()
if mail_message['isDraft']:
print('Subject:',mail_message['subject'])
print('To:',mail_message['toRecipients'])
print('Is Read:',mail_message['isRead'])
print('Received Date Time:',mail_message['receivedDateTime'])
print()
else:
print('Subject:',mail_message['subject'])
print('To:',mail_message['toRecipients'])
print('From:',mail_message['from']['emailAddress']['name'],f"({mail_message['from']['emailAddress']['address']})")
print('Is Read:',mail_message['isRead'])
print('Received Date Time:',mail_message['receivedDateTime'])
print()
print('-'*150)
except httpx.HTTPStatusError as e:
print(f'HTTP Error: {e}')
except Exception as e:
print(f'Error: {e}')
main()