Posts: 2
Joined: Wed Jan 25, 2017 2:53 pm

Motion detection, email notification

Thu Feb 09, 2017 5:29 pm

Hi All,

My project consists of one RPi 3 with motion sensor and camera attached. All this is linked to my gmail account. Once motion is detected, it will take a recording and email me saying that motion has been detected and it should also attach the image. All this happens automatically. I have ssmtp all set up for my gmail and everything works perfect, apart from the image is not attached to the email. So, I get the notification, but with no image attached. Please help. Please see my script below and please advice as I am stuck.


import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import smtplib
import os

AuthUser = 'myemail@gmail.com'
AuthPass = 'mypassword'

toAdd = 'myemail@gmail.com'
fromAdd = AuthUser

subject = 'Motion Detected'

def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

mail = smtplib.SMTP('smtp.gmail.com',587)

gmailUpload = ()



mail.login(AuthUser, AuthPass)

mail.send_mail = (fromAdd, toAdd)

#sensor setup

sensorPin = 7

GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

camera = picamera.PiCamera()

while True:
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
newState = "HIGH" if currState else "LOW"
print ("GPIO pin %s is %s" % (sensorPin, newState))
if currState:
fileName = getFileName()
print ("Starting Recording...")
print (fileName)
print ("Stopped Recording")
print ("Sending Mail Notification...")
subprocess.call("mail -s 'Motion Detected' myemail@gmail.com < /home/pi/mail/images", shell=True)
print ("Complete")

Posts: 10119
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Motion detection, email notification

Thu Feb 09, 2017 9:33 pm


I use ssmtp to send my daily speed test results file from my broadband speed testing Pi.

To do this I found I had to use mpack with ssmtp

http://ozzmaker.com/send-email-from-the ... tachments/

then I send the file like this

Code: Select all

mpack -s "Broadband Speed Testing Results with txt file attached" -d /home/pi/message.txt /home/pi/speed_test_results.txt $email_addresses
so the bit between " " is the subject line
-d /home/pi/message.txt his file contains the actual message text.
the last bit /home/pi/speed_test_results.txt is the file you want to send.
after this you have the address you want to send too.

hope this helps
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Posts: 3
Joined: Thu Feb 09, 2017 8:40 pm

Re: Motion detection, email notification

Thu Feb 09, 2017 10:26 pm

One of my to do list items, email send from RPi, thanks for posting!!!

Posts: 1
Joined: Sun Oct 15, 2017 2:16 pm

Python code for motion detection and mail sending

Sun Oct 15, 2017 2:26 pm

Please give me the python code to detect motion using PIR sensor, capture image and send mail with the image captured.
Kindly do the needful.

Posts: 410
Joined: Tue Oct 14, 2014 5:21 pm

Re: Motion detection, email notification

Sun Oct 15, 2017 5:47 pm

Here is something I can recommend
The goal here is to make it as simple and painless as possible to send emails
You can basically send email with gmail.with one line. It also does not save your password in the script, but uses the keyring

https://github.com/kootenpv/yagmail/blo ... /README.md

Return to “Advanced users”