-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsend_email_button.py
63 lines (49 loc) · 2.15 KB
/
send_email_button.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
#!/usr/bin/python
import os
from time import sleep
import RPi.GPIO as GPIO
import base64
import httplib2
from time import gmtime, strftime
from email.mime.text import MIMEText
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
EMAIL = "[email protected]" # change this
# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = '/usr/share/adafruit/webide/repositories/my-pi-projects/Doorbell/client_secret_935106472194-ntvvnbqtgcpnd2qu7akrk6g8tu225br4.apps.googleusercontent.com.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.compose'
# Location of the credentials storage file
STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# create a message to send
message = MIMEText(
"Hi, \n\nSomeone knocked on your door at" + strftime("%l:%M %p on %d-%m-%Y") + ".\n\nHave a great day!")
message['to'] = EMAIL
message['from'] = "[email protected]"
message['subject'] = "Ding Dong at " + strftime("%l:%M %p on %d-%m-%Y")
body = {'raw': base64.b64encode(message.as_string())}
while True:
if not GPIO.input(14): # button pressed
# send it
try:
message = (gmail_service.users().messages().send(userId="me", body=body).execute())
print('Message Id: %s' % message['id'])
print(message)
except Exception as error:
print('An error occurred: %s' % error)