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

Required software

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.

Notification scripts

Example bash script with inline status information

create file in nano

nano email_inline.sh

Add this code and then save

#!/bin/bash
# script to email status information inline

# get the single digit of time, ie 0-9 for file extension
taken=`date "+%a_%x_%R"`

#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 -- recipient.address@your.domain

Now make it executable

chmod a+x email_inline.sh

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

Example bash script with attached status file

create file in nano

nano email_attachment.sh

Add this code and then save

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

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

# get the single digit of time, ie 0-9 for file extension
taken=`date "+%a_%x_%R"`

echo -n | mutt -a "/home/pi/pi_status.log" -s "RPi logfiles "$taken -- recipient.address@your.domain

rm -f /home/pi/pi_status.log

Now make it executable

chmod a+x email_attachment.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