-
Notifications
You must be signed in to change notification settings - Fork 1
/
GmailAPi_send_Mail.py
49 lines (36 loc) · 1.34 KB
/
GmailAPi_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
from apiclient.discovery import build
from apiclient import errors
from httplib2 import Http
from oauth2client import file, client, tools
from email.mime.text import MIMEText
from base64 import urlsafe_b64encode
SENDER = "[email protected]"
RECIPIENT = "[email protected]"
SUBJECT = "mine"
CONTENT = "Hero is great u idiot"
SCOPE = 'https://www.googleapis.com/auth/gmail.compose'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('cer.json', SCOPE)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
def create_message(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
encoded_message = urlsafe_b64encode(message.as_bytes())
return {'raw': encoded_message.decode()}
def send_message(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print('Message Id: %s' % message['id'])
return message
#except errors.HttpError, error:
except(errors.HttpError):
print('ERROR LOG' , errors.HttpError)
raw_msg = create_message(SENDER, RECIPIENT, SUBJECT, CONTENT)
print(raw_msg)
send_message(service, '[email protected]', raw_msg)