1. Install Mutt email program
sudo apt-get install mutt cyrus-common libsasl2-2 libsasl2-modules libsasl2-modules-db imagemagick img2pdf
mkdir -p ~/.mutt/cache/headers
mkdir ~/.mutt/cache/bodies
touch ~/.mutt/certificates
Edit: 15.4.2025: Old instructions does not work anymore for mutt so here is new instructions for mutt
2. Make a .muttrc file
# ================ IMAP =========
set editor = "nano" # Set your favourite editor.
set edit_headers = yes # See the headers when editing
set charset = UTF-8 # value of $LANG; also fallback for send_charset
set imap_user = johndoe@jotain.com # use real user
set imap_pass = password-here # use real password
set spoolfile = imaps://imap-server-here/INBOX # use real imap server
set folder = imaps://imap-server-here/ # use real imap server
set record="imaps://imap-server-here/Sent Mail" # use real imap server
set postponed="imaps://imap-server-here/Drafts" # use real imap server
set mbox="imaps://imap-server-here/All Mail" # use real imap server
set header_cache = "~/.mutt/cache/headers"
set message_cachedir = "~/.mutt/cache/bodies"
set certificate_file = "~/.mutt/certificates"
# ================ SMTP =============
## Send options.
set smtp_url=smtps://email-address@jotain.com:password-here@smtp-server-here:465/ # use real smtp server
set realname='User X' # use real name
set from=johndoe@jotain.com # use real address
set hostname="mutt@mutt" # use some hostname
set signature="John Doe" # # use some name
# Connection options
set ssl_force_tls = yes
unset ssl_starttls
# =====================================
## After installing Mutt, there need to run Mutt via command line first and write and send a test email for getting sertificates.
# ====================================
# If getting an authentication error when sending an attachment, install postifix package and choose ”local” as internet operator.
=====================================
3. Make two folders
mkdir -p $HOME/motion
mkdir -p $HOME/Kuvat/posti
4. Install motion
sudo apt install motion
Edit /etc/motion/motion.conf file and make two scripts where the first script lauches Mutt if Motion has created pictures.
====================================
check-motion-folder script
#!/bin/bash
# RJP 15.4.2025 loop test if motion folder has pictures
# Gap in seconds of no motion detected that triggers the end of an event.
#event_gap 30
## /home/username/motion is the path for motion pictures
# Target directory for pictures, snapshots and movies
#target_dir /home/username/motion
# Picture output configuration parameters
###################################
# Output pictures when motion is detected
#picture_output on
###################################
# Create movies of motion events.
#movie_output off
##################################
while true; do
if [ -z "$( ls -A '/home/username/motion' )" ]; then
echo "Empty"
else
echo "Not Empty"
## ./run-mutt # this option can choke imagemagick
# ./frefress-motion-folder ## if there has no internet connection
# creating backup from $HOME/motion daily
./run-mutt-pdf
fi
sleep 50
done
=================================
run-mutt script
#!/bin/bash
# RJP 15.4.2025 run mutt if pictures are detected
aika=$(date "+%F-%H-%M-%S")
mkdir -p /home/username/Kuvat/posti
mv /home/username/motion/*.jpg /home/username/Kuvat/posti && cd /home/username/Kuvat/posti && convert -append *.jpg valvontakuva.jpg
mutt -s "Valvontakuvia" -a /home/username/Kuvat/posti/valvontakuva.jpg -- email-to-send@jotain.com < /dev/null
sleep 1 && mv /home/username/Kuvat/posti /home/username/Kuvat/posti-$aika
==================================
If imagemagick makes a too tall or wide picture, it can not convert it as a one picture, so better to convert pictures to pdf-file.
run-mutt-pdf script
#!/bin/bash
# RJP 17.4.2025 run mutt if pictures are detected
# sendind as pdf if imagemagick chokes
aika=$(date "+%F-%H-%M-%S")
mkdir -p /home/username/Kuvat/posti
mv /home/username/motion/*.jpg /home/username/Kuvat/posti && img2pdf /home/username/Kuvat/posti/*.jpg -o /home/username/Kuvat/posti/valvontakuva.pdf && sleep 3 && \
mutt -s "Valvontakuvia" -a /home/username/Kuvat/posti/valvontakuva.pdf -- email-address@jotain.com < /dev/null && sleep 1 && \
rm /home/username/Kuvat/posti/valvontakuva.pdf && sleep 1 && mv /home/username/Kuvat/posti /home/username/Kuvat/posti-$aika
=================================
5. Set the scripts executable and add check-motion-folder to the startup if needed.
chmod +x check-motion-folder
chmod +x run-mutt
chmod +x run-mutt-pdf
chmod +x frefress-motion-folder
To test if scrip or program is running
check-process-state script
#!/bin/bash
# RJP 18.4.2025 check-motion-folder running test
x-terminal-emulator -e /bin/bash -c '(ps -ef|grep -v grep|grep check-motion-folder > /dev/null && echo "check-motion-folder is running" || echo "check-motion-folder is not running") ; exec bash' && read -p \"
#################################################
########## running test ######################
#################################################\"" &
#==============================
To stop check-motion-folder script
#!/bin/bash
killall check-motion-folder
start-motion
#!/bin/bash
# RJP 18.4.2025 start motion manually if is not set in rc.local , etc
x-terminal-emulator -e /bin/bash -c "sudo motion && read -p \"
#################################################
########## motion stopped ######################
#################################################\"" &
#============================
stop-motion script
#!/bin/bash
# RJP 18.4.2025 stop motion manually
x-terminal-emulator -e /bin/bash -c "sudo killall motion && read -p \"
#################################################
########## motion should be stopped ######################
#################################################\"" &
#===========================
# If wanting to start and motion as regular user, edit sudoers file like
# username-here ALL=(ALL) NOPASSWD:/usr/bin/motion # works with scripts where has "sudo"
#==========================
if there has no internet connection, then creating backup from $HOME/motion daily.
This option need to set on by commenting out (removing #) from check-motion-folder script (./frefress-motion-folder).
frefress-motion-folder script
#!/bin/bash
# RJP 19.4.2025 # ./frefress-motion-folder ## if there has no internet connection
# creating backup from $HOME/motion daily
aika=$(date "+%Y-%m-%d")
cd $HOME/motion && \
ls -l | awk '{print $6,$7,$8,$9}' && \
stat -c %y $HOME/motion | cut -d" " -f1 > /tmp/vanha && \
date "+%Y-%m-%d" > /tmp/nykyinen && \
pwd && \
if [[ $(cat /tmp/vanha) == $(cat /tmp/nykyinen) ]]
then
echo "today created motion folder"
else
echo "old motion folder, creating new and backup from old"
mv $HOME/motion $HOME/motion-$aika && mkdir -p $HOME/motion
fi
#============================
###################################
### OLD instructions from year 2015 ###
###################################
set imap_user = from_my@gmail.com
set imap_pass = verygoodpasswordhere
set smtp_url = smtp://from_my@smtp.gmail.com:587/
set smtp_pass = verygoodpasswordhere
set from = from_my@gmail.com
set realname = seuritycamera
set folder = imaps://imap.gmail.com:993
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set record = '+Sent'
3. Test Mutt before installing Motion and then install Motion
sudo apt install motion
Add yourself to the motion group
sudo adduser myself motion
Configure Motion
gksu pluma /etc/default/motion
# set to 'yes' to enable the motion daemon
start_motion_daemon=yes
gksu pluma /etc/motion/motion.conf
... edit these lines
--------------------------------
# Minimum time in seconds between capturing picture frames from the camera.
# Default: 0 = disabled - the capture rate is given by the camera framerate.
# This option is used when you want to capture images at a rate lower than 2 per second.
minimum_frame_time 1
--------------------------------
# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
target_dir /path/motion
--------------------------------
# Command to be executed when a motion frame is detected (default: none)
on_motion_detected /home/username/.startup.sh
--------------------------------
4. Make a startup script
#! /bin/sh
Make the script executable
chmod a+x /home/username/.startup.sh
#! /bin/sh
TIMESTAMP=$(date "+%F-%H-%M-%S")
find /path/motion -iname "*.swf" -exec mv {} /path/allvideos \; && sleep 30 && genisoimage -o /path/toemail/video$TIMESTAMP.iso /path/allvideos && mutt -z -a /path/toemail/video$TIMESTAMP.iso -s "Security videos" -- to_some_email@gmail.com from_my@gmail.com < /dev/null
Make the script executable
chmod a+x /home/username/.mutt.sh
/path/motion
/path/allvideos
/path/toemail
Set the permissions for the folders
sudo chmod 775 /path/foldername
6. Add Motion to the startup applications and or crontab
PS. emails comes always when Motion has react!
WordPress shows some lines wrong, so use this document.
See also Motion live stream via browser
