-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend_mail.py
143 lines (119 loc) · 3.71 KB
/
send_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import json
import time
import requests
import threading
from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
# Configure this.
FROM_EMAIL_ADDR = '[email protected]'
TO_EMAIL_ADDR = '[email protected]'
REDIRECT_URL = 'http://127.0.0.1:5000/callback/'
CLIENT_ID = ''
CLIENT_SECRET = ''
BASE_OAUTH_API_URL = 'https://accounts.zoho.eu/'
BASE_API_URL = 'https://mail.zoho.eu/api/'
ZOHO_DATA = {
"access_token": "",
"refresh_token": "",
"api_domain": "https://www.zohoapis.eu",
"token_type": "Bearer",
"expires_in": 3600,
"account_id": ""
}
def req_zoho():
url = (
"%soauth/v2/auth?"
"scope=ZohoMail.messages.CREATE,ZohoMail.accounts.READ&"
"client_id=%s&"
"response_type=code&"
"access_type=offline&"
"redirect_uri=%s"
) % (BASE_OAUTH_API_URL, CLIENT_ID, REDIRECT_URL)
print('CLICK THE LINK:')
print(url)
print('This only has to be done once.')
def get_access_token(code):
state = request.args.get('state')
url = '%soauth/v2/token' % BASE_OAUTH_API_URL
data = {
'code': code,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URL,
'scope': 'ZohoMail.messages.CREATE,ZohoMail.accounts.READ',
'grant_type': 'authorization_code',
'state': state
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=data, headers=headers)
data = json.loads(r.text)
ZOHO_DATA['access_token'] = data['access_token']
def get_account_id():
url = BASE_API_URL + 'accounts'
headers = {
'Authorization': 'Zoho-oauthtoken ' + ZOHO_DATA['access_token']
}
r = requests.get(url, headers=headers)
data = json.loads(r.text)
ZOHO_DATA['account_id'] = data['data'][0]['accountId']
def send_mail(body, email_address):
url = BASE_API_URL + 'accounts/%s/messages'
url = url % ZOHO_DATA['account_id']
data = {
"fromAddress": FROM_EMAIL_ADDR,
"toAddress": email_address,
"ccAddress": "",
"bccAddress": "",
"subject": "Test E-Mail",
"content": body,
"askReceipt": "no"
}
headers = {
'Authorization': 'Zoho-oauthtoken ' + ZOHO_DATA['access_token']
}
r = requests.post(url, headers=headers, json=data)
print(r.text)
def refresh_auth():
# Update the access token every 50 minutes using the refresh token.
# The access token is valid for exactly 1 hour.
time.sleep(10)
while True:
url = (
'%soauth/v2/token?'
'refresh_token=%s&'
'client_id=%s&'
'client_secret=%s&'
'grant_type=refresh_token'
) % (BASE_OAUTH_API_URL, ZOHO_DATA['refresh_token'], CLIENT_ID, CLIENT_SECRET)
r = requests.post(url)
data = json.loads(r.text)
if 'access_token' in data:
ZOHO_DATA['access_token'] = data['access_token']
print('refreshed', ZOHO_DATA)
time.sleep(3000) # 50 minutes
else:
# Retry after 1 minute
time.sleep(60)
@app.route('/callback/', methods=['GET', 'POST'])
def zoho_callback_route():
code = request.args.get('code', None)
if code is not None:
get_access_token(code)
get_account_id()
return 'OK', 200
@app.route('/sendmail/', methods=['GET', 'POST'])
def send_mail_route():
# Send a HTML email!
data = ['1', '2', '3']
mail = render_template('mail_template.j2', data=data)
send_mail(mail, TO_EMAIL_ADDR)
return 'OK', 200
def main():
req_zoho()
t = threading.Thread(target=refresh_auth)
t.start()
app.run(host='0.0.0.0')
if __name__ == '__main__':
main()