from picamera import PiCamera
from time import sleep
import smtplib
import time
from datetime import datetime
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import RPi.GPIO as GPIO
import time

toaddr = 'xxxxxxxxxxx@gmail.com' #  receivers email id
me = 'xxxxxxxxxxx@gmail.com'  # senders email id
Subject='security alert'

GPIO.setmode(GPIO.BCM)

P=PiCamera()
P.resolution= (320,240)
P.start_preview()
    
GPIO.setup(23, GPIO.IN)
while True:
    if GPIO.input(23):
        print("Motion...")
        #camera warm-up time
        time.sleep(2)
        P.capture('movement.jpg')
        time.sleep(10)
        subject='Security allert!!'
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = me
        msg['To'] = toaddr
        
        fp= open('movement.jpg','rb')
        img = MIMEImage(fp.read())
        fp.close()
        msg.attach(img)

        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(user = 'xxxxxxxxxxx@gmail.com',password = 'xxxxxxxxx') #email id and passwords of senders
        server.sendmail(me, toaddr, msg.as_string())
        server.quit()
        P.stop_preview()