Dubman
Posts: 118
Joined: Thu Mar 15, 2018 3:53 pm

python email alert images accumulating

Tue Mar 19, 2019 10:29 pm

Hi, I have the below code which is working and sends me email alerts when a PIR sensor detects
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()
Thanks

Paul Hutch
Posts: 370
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: python email alert images accumulating

Tue Mar 19, 2019 11:00 pm

I've never worked with attachments but I'd try clearing the variable after sending.

Code: Select all

        server.send_message(msg)
        server.quit()
	msg = None

Dubman
Posts: 118
Joined: Thu Mar 15, 2018 3:53 pm

Re: python email alert images accumulating

Wed Mar 20, 2019 7:14 pm

Thanks for that tip, but unfortunately the result of adding the msg = none was that no mail was sent at all (unless I have some other issues that may complicate things).

Code: Select all

server.send_message(msg)
        server.quit()
	msg = None  
Subsequently I've had other problems and decided it would be easier to reinstall Raspbian on the sd card.
Any other ideas to try?
Thank you

Paul Hutch
Posts: 370
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: python email alert images accumulating

Wed Mar 20, 2019 10:40 pm

Ah, I guess the "server.quit()" doesn't actually quit the server when called (multi-threading issue) so the clearing of the "msg" email content variable is happening before the email has been sent. So my next guess is that doing the clearing of the old email content needs to be moved to just before creating the email.

Code: Select all

        subject='Security allert!!'
        msg = None
        msg = MIMEMultipart()

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12041
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: python email alert images accumulating

Thu Mar 21, 2019 12:35 pm

Please don't use two threads to ask one question, locking this thread, continue with the other one....viewtopic.php?f=63&t=235999&p=1443102#p1443073
This thread will be deleted in a few days.

Return to “General discussion”