Oct 242016
 

A while ago, I setup my Motion server to send me snapshots of any motion detected when a picture is saved by motion.

Recently, I’ve found that the Motion server sends me so many emails that I hit the limit of emails received per minute, so I’ve had to redesign the script that I was using to send screencaps to myself.

Instead of using on_picture_save to run a command to send an email, I’ve set up my motion configuration to run a script via the on_event_end. This script then finds the last couple of minutes of snapshots, attaches them to an email, then sends the email off.

To implement this, I needed to comment out the on_picture_save line so that it doesn’t keep sending screencaps when a photo is saved.
After that line is commented out, I added a line for motion to run a command when a motion event ends.

on_event_end /opt/scripts/motion/sendbulk.sh

I then created the script that I named in the line above. You’ll need to know the location of the snapshots that motion takes for this script. The location is set in the target_dir configuration option.

The script uses the find command to find pictures that have been created in the last couple of minutes, and attach them to the email.
The heirloom-mailx mail client is *REQUIRED* for this script as it uses that particular client’s mail attachment command.

The script below should be saved as sendbulk.sh in the /opt/scripts/motion directory, unless you have changed where the script is located in the configuration option mentioned earlier.

#!/bin/bash
# This needs heirloom-mailx
location=/var/lib/motion # Sets the location for the motion snapshots
to="myself@example.com" # Set your email here.
subject="Motion Detected" # The Email Subject
body="Motion detected" # The Email Body

# Finding the screencaps
declare -a attargs
for att in $(find $location -cmin -2 -type f -name "*.jpg"); do
attargs+=( "-a" "$att" )
done

mail -s "$subject" "${attargs[@]}" "$to" <<< "$body" # Send the actual email

Once the script has been created, ensure it is executable by running chmod +x /opt/scripts/motion/sendbulk.sh
After this is done, you should start receiving emails with more than one attached picture.

Reference:
http://unix.stackexchange.com/questions/138061/attach-files-for-sending-mail-which-are-the-result-set-of-find-command

Share

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)