movement. The issue I have is that if I get a few different email alerts, the jpg images are accumulating in the emails. The first alert has the correct number of images, but the images accumulate and, say in the tenth email I seem to also have attached some (or all?) images from the previous mails. Is there a way to avoid this?
I got this code from https://iotbyhvm.ooo/raspberry-pi-home- ... -tutorial/
Code: Select all
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 = 'TO_EMAIL_address' me = 'FROM_EMAIL_address' Subject='alert' GPIO.setmode(GPIO.BCM) P=PiCamera() P.resolution= (1024,768) 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 = 'FROM_EMAIL',password='PASSWORD') server.send_message(msg) server.quit()