-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailOperations.py
94 lines (75 loc) · 3.42 KB
/
emailOperations.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
import requests
from bot_token import authenticate_bot
from access_token import authenticate_user
import os
# {subject,body, content, recipient}
def send_emails(message):
headers = {"Authorization": str(authenticate_bot()['access_token'])}
response = requests.get("https://graph.microsoft.com/v1.0/me", headers=headers)
if response.status_code == 200:
print("Authenticated successfully")
else:
# Handle error
print("Error authenticating")
data = {
"message": {
"subject": message['subject'],
"body": {
"content": message['content']
},
"toRecipients": [
{
"emailAddress": {
"address": message['recipient']
}
}
]
}
}
response = requests.post("https://graph.microsoft.com/v1.0/me/sendmail", json=data, headers=headers)
if response.status_code == 202:
print("Email sent successfully")
else:
# Handle error
print("Error sending email",response.content)
exit(1)
def read_emails():
headers = {"Authorization": str(authenticate_bot()['access_token'])}
# Retrieve a list of emails in the Inbox folder
response = requests.get("https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages", headers=headers)
if response.status_code == 200:
email = response.json()["value"][0]
email_id = email["id"]
response = requests.get(f"https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/{email_id}", headers=headers)
if response.status_code == 200:
email_details = response.json()
attachment_response = requests.get(f"https://graph.microsoft.com/v1.0/me/messages/{email_id}/attachments", headers=headers)
attachments = []
attachment_contents = {}
if attachment_response.status_code == 200:
attachments = attachment_response.json().get('value', [])
for attachment in attachments:
attachment_id = attachment['id']
attachment_detail_response = requests.get(f"https://graph.microsoft.com/v1.0/me/messages/{email_id}/attachments/{attachment_id}/$value", headers=headers)
if attachment_detail_response.status_code == 200:
attachment_contents[attachment['name']] = attachment_detail_response.content
else:
print(f"Error retrieving attachment content for {attachment['name']}")
# Extract only the name without the extension
attachment_names = [os.path.splitext(attachment['name'])[0] for attachment in attachments]
# print(email_details)
return {
'from': email_details['from'],
'subject': email_details['subject'],
'body': email_details['bodyPreview'],
'received': email_details['receivedDateTime'],
'attachments': attachment_names,
# 'attachment_contents': attachment_contents
}
else:
print("Error retrieving email details")
else:
print("Error retrieving emails")
# message= {'subject' : "1234",'content': " Hello test email", "address": "[email protected]"}
# send_emails('[email protected]',message)
# print(read_emails('[email protected]'))