-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailSender.py
74 lines (53 loc) · 1.87 KB
/
emailSender.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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
import os
def sendEmail():
os.system("sudo fswebcam -r 300x200 image.jpeg")
email = 'stayawaythieves@gmail.com'
password = 'hahayoutried'
send_to_email = 'stayawaythieves@gmail.com'
subject = 'THIEF DETECTED'
message = 'Potential Thief has tried to open your box!'
file_location ='/home/pi/Downloads/image.jpeg' #remember to save python file in this directory as the photo will also be saved here
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
# Setup the attachment
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
#!/usr/bin/env python
import RPi.GPIO as GPIO
channel = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def alert(ev = None):
print ("Tilt Detected")#for debugging purposes
sendEmail()
def loop():
GPIO.add_event_detect(channel, GPIO.FALLING, callback = alert, bouncetime = 100)
while True:
pass
if __name__ == '__main__':
try:
loop()
except KeyboardInterrupt:
GPIO.cleanup()
'''