Send email notifications

Using something like a Raspberry Pi is great, but it often means its located in a hard to get at place, ( top of a hangar ! ), or you have no remote access.

It would be useful if the RPi sent you a regular update of its status etc.

If you are using DHCP, then its somewhat tricky to connect to the picam on the local wlan, as you dont know the ip, but if the RPi could tell you…

This can be done easily using email.

This is a basic guide to setting up email, using Gmail, ( rather than your ISP mail server ), on your RPi, based around these links and some work I did..

use-gmail-as-a-smarthost
From raspberrypi.org

First install the ssmtp and mutt libraries.

sudo apt-get update
sudo apt-get install ssmtp
sudo apt-get install mutt

Create a 'new' Gmail account
make a note of the username/password as you'll need this for your RPi to send emails.

Configure the /etc/ssmtp/ssmtp.conf file to your gmail settings.

nano /etc/ssmtp/ssmtp.conf

change the line that includes mail hub to,

mailhub=smtp.gmail.com:587

add these lines

AuthUser=your.account@gmail.com
AuthPass=Your-Gmail-Password

FromLineOverride=YES
UseSTARTTLS=YES

and save…

You should now be able to send emails from your RPi.

You can then send emails from cmd line and hence from a bash script..

Either using ssmtp

echo "this is a test" | ssmtp -s Subject your.email@your.domain

or mutt

echo "Email from your RPi “ | mutt -a "/home/pi/ocr_pi.png" -s "Insert your subject here "$taken -- your.email@your.domain

the ‘echo’ bit is what appears in the body of the email
the –a bit is to add an attachment
the –s is the subject….

Its now a 'relatively trivial' task to use a bash script to send an email with whatever contents you want,
eg local ip, logfiles, disk usage etc and then call that from crontab to send you a daily email.


Example bash script to email mini healthcheck

create file in nano

nano health.sh

Add this code and then save

#!/bin/bash

# script to email logfiles 
# then delete the logfiles to save space

ifconfig eth0 > picam_ip.log
df -v >> picam_ip.log
lsusb >> picam_ip.log

# get the single digit of time, ie 0-9 for file extension
day=`date "+%a"`
DMY=`date "+%x"`
now=`date "+%R"`
taken=$day"_"$DMY"_"$now

gettime=`date "+%M"`

#insert healthcheck commands here
{
echo "####################   IP settings  ##################"
echo "           "
ifconfig eth0
echo "           "
echo "####################   disk usage  ##################"
echo "           "
df -v
echo "           "
echo "####################   usb devices ##################"
echo "           "
lsusb
echo "           "
ls -lrt
} | mutt -s "RPi Health "$taken -- your.email@your.domain

rm -f /home/pi/picam_ip.log

Now make it executable

chmod a+x email.sh

Then add it to your crontab ( notes on that to follow, or use MAN ;) )


Example bash script to email logfiles ( called picam1.log )

create file in nano

nano email.sh

Add this code and then save

#!/bin/bash
# script to email logfiles
# then delete the logfiles to save space

# get the single digit of time, ie 0-9 for file extension
day=`date "+%a"`
DMY=`date "+%x"`
now=`date "+%R"`
taken=$day"_"$DMY"_"$now

gettime=`date "+%M"`

{
cat /home/pi/picam1.log
} | mutt -a "/home/pi/picam1.log" -s "RPi logfiles "$taken -- your.email@your.domain

rm -f /home/pi/picam1.log

Now make it executable

chmod a+x email.sh

Finally, add it to your crontab

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License