3. Email Notification

The core part of the project is actually ready. If all is configured correctly, the presence of an intruder will be detected automatically, and the recorded videos will be saved on the local drive (SD card). However, we may not get the access to our board in order to retrieve the files or simply, we'd like to know sooner. In order to transfer the files, I use Part II from this tutorial as it provides an easy way for future expansion and excellent explanation, except for scheduling the activity of Motion using crontab.

General idea

The general idea of this approach is to "emulate" a mail box using the Raspberry and send an email to oneself using the attachment (recorded videos of the intruder). I will recall the instruction for the sake of completion, but it essentially comes from the link I stated above.

As you remember, the on_event_end line inside motion.conf file provides the "entry point" here (or exit rather... nevermind). By typping sudo zip_and_mail.sh there, we are going to execute the script, which will collect all the content of the INBOX directory, compress it and send it to us by email.

My customised implementation

In my implementation, I have modified the script slightly in order to fit with my needs better. I do not use the startmotion.sh and stopmotion.sh scripts as instructed. Instead, I only use the zip_and_send.sh.

Following the link, I assume that you have installed the ssmtp and mutt packages as well as configured them just as stated therein. Now, our task is to complete the code of zip_and_send.sh.

Important! Gmail is constantly improving the security, so in order to actually make it work using Gmail, make sure that you lower the security level for the email account. Personally, I advice you to create a separate and dedicated account for this job.

The content of the zip_and_send.sh script in my implementation is the following:


message="Thief entered your living room."

echo "Target file path: $INBOX"
echo "Running zip_and_send.sh..." `date`
numfiles=$( ls -l $INB0X | egrep -c '^-')

numfolders=`expr $numfiles / $splitsize`
lastfoldercount=`expr $numfiles % $splitsize`

while [ $COUNTER -le $numfolders ]; do
 echo "Creating directory... " `date`
 echo "Directory $DESTINATION created."
 sudo rm -rf $DESTINATION
 sudo mkdir -p $DESTINATION

 if [ "$COUNTER" -lt "$numfolders" ]; then
  for file in $(ls -p $INBOX | grep -v / | tail -$splitsize); do
   echo "File to be moved: $INBOX/$file to: $DESTINATION"
   sudo mv $INBOX/$file $DESTINATION
  for file in $(ls -p $INBOX | grep -v / | tail $lastfoldercount); do
   echo "File to be moved: $INBOX/$file to: $DESTINATION"
   sudo mv $INBOX/$file $DESTINATION

 echo "Creating zip file... " `date`
 sudo zip -9 -r -q $OUTBOX/motion$COUNTER.zip $DESTINATION/*

 # sending email
 echo "Sending email to the recipient: $recipient" `date`
 echo $message | mutt -s "Catch the Thief!" $recipient -a $OUTBOX/motion$COUNTER.zip

 # cleaning the old files
 echo "Removing file: $OUTBOX/motion$COUNTER"
 sudo rm $OUTBOX/motion$COUNTER.zip
 sudo rm $DESTINATION/*
 sudo rmdir $DESTINATION
 echo "Removing $OUTBOX/motion$COUNTER"
 sudo rmdir $OUTBOX/motion$COUNTER


Here, remember to create the INBOX and OUTBOX directories before executing this script. The zip_and_send.sh must also be executable (permissions!). If everything is done correctly, warning emails should start appearing in your mailbox as soon as some movement is detected.

left right