Sending alert emails from a Raspberry Pi for home automation projects

Sending alert emails from a Raspberry Pi for home automation projects
Published: by
Last updated:
Categories
Raspbian

The Raspberry Pi can be used to send email messages. This, in combination with APIs and Cron, can be used as a powerful alert system.

Introduction

This post shows how to send emails with a Raspberry Pi using terminal commands. Alert emails can also be configured to be sent by Cron.

When compared with SMSs, sending and receiving emails only require a very small amount of data (with no hidden or prepaid costs in the form of email bundles for example).

When specifically focussing on the average time it might take to receive an email (as determined by the email reader’s checking interval), SMSs are often much faster — an important consideration when the alerts are for example when an alarm is triggered.

Assumptions

For this post, a fully installed Raspberry Pi with Raspbian connected to the internet was used. Without a connected keyboard and screen, PuTTY and/or WinSCP can be used to do the testing and coding. From the API side, a verified Gmail account was also required. The Gmail address will be used in the ‘From:’ address and the account will handle the actual sending of emails.

Installing the required packages

To send emails from a Raspberry Pi, two additional packages (sSMTP and mailutils) is required. sSMTP is a lightweight SMTP (simple mail transfer protocol) server used to send emails. Mailutils is a set of libraries for handling emails. To install these packages the following terminal commands can be used:

sudo apt-get install ssmtp
sudo apt-get install mailutils

Configuring sSMTP

Make a backup of the original ssmtp.conf file:

sudo cp -p /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.original

Open and edit the configuration file:

sudo nano /etc/ssmtp/ssmtp.conf

Scrolling down a bit, change the mailhub from mail to:

mailhub=smtp.gmail.com:587

and add the following lines (with your own details):

AuthUser=name@gmail.com
AuthPass=yourpassword
UseSTARTTLS=YES
UseTLS=YES

Press Ctrl + X, then Y to save and exit.

Sending an email

The basic structure of sending an email from the terminal is:

echo "Body text here." | mail -s "Subject text here." sendto@email.com

To send a test email use:

sudo echo "Test email" | mail -s "Testing ssmtp setup" your@email.com

Emails can be sent to any address, not only @gmail addresses.

Tips & tricks

Because the entire email is sent using a one-line command, the paragraph and special character layout can be a bit tricky to incorporate in the terminal. These shortcomings can be overcome by using Bash. To create a Bash script, use the following terminal command:

sudo nano /home/pi/email.sh

Add the following to it:

#!/bin/bash
# Bash sample code to help with string paragraph and some special character layout

new_line="\n"
horisontal_line="______________________________________________"
double_quote=\"
single_quote=\'

Don’t exit yet.

These example strings can be used to create paragraphs and add some special characters. Others can also be added and used.

Add the following to the bottom of the email script before exiting and saving (Ctrl + X, Y).

echo -e "First paragraph before break.$new_lineFirst paragraph after break.$new_line$new_lineSecond paragraph$new_line$new_line$double_quoteQuote...$double_quote$new_line$horisontal_line" | mail -s "Testing paragraph layout with Bash" your@email.com

Note the -e after the echo.

Test the email script:

sudo /home/pi/email.sh

Using sSMTP and Cron

When sSMTP is installed on a system that uses Crontab to schedule tasks, Crontab will automatically try to send any output messages generated by each entry/task. Output messages are either in the form of standard messages/intended output (stdout) or error messages (stderr).

By default, the email address specified in the AuthUser section of the ssmtp.conf file will be used as the recipient. To change this, Cron can be instructed to use one or more different email addresses instead.

To instruct Cron to send all messages to only one email address, a MAILTO field can be inserted in the Crontab file. To add this field, enter Crontab from the terminal:

crontab -e

and add the following line anywhere – replacing it with a real email address:

MAILTO="sendtoyour@email.com"

Alternatively, each Cron entry can have its own email address defined by simply adding the mail command at the end of the specific Crontab entry:

* * * * * command | mail -s "Subject text here." sendto@email.com

To prevent Cron from sending any emails, sSMTP should either be removed/uninstalled:

sudo apt-get --purge remove ssmtp

or its output needs to be ‘piped’ to /dev/null. To do that, one of the following lines can be added to the end of the specific Crontab entry:

> /dev/null

to prevent the output (stdout) to be emailed, or:

> /dev/null 2>&1

to prevent the stdout and error messages (stderr) to be emailed.

Use Ctrl + X, Y to save Crontab and exit.

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause, or becoming a Patron for exclusive content.
Donate via PayPal Become a Patron

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Raspbian related posts