import os import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart import picamera import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.IN) subject = "An intruder has been detected" to = "Google Username" UserName = to UserPassword = "Google Password" camera = picamera.PiCamera() def SendMail(ImgFileName): img_data = open(ImgFileName, 'rb').read() msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = to msg['To'] = to text = MIMEText("Here is an attached image of the intruder") msg.attach(text) image = MIMEImage(img_data, name=os.path.basename(ImgFileName)) msg.attach(image) s = smtplib.SMTP("smtp.gmail.com:587") s.ehlo() s.starttls() s.ehlo() s.login(UserName, UserPassword) s.sendmail(to, to, msg.as_string()) print("Sending mail") s.quit() def takePicture(channel): camera.capture('image.jpg') SendMail('image.jpg') os.remove('image.jpg') sleep(20) GPIO.add_event_detect(4, GPIO.RISING, callback=takePicture, bouncetime=500) while True: pass