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:
Raspberry Pi

On the Raspberry Pi, alert messages can be sent using various communication mediums (e.g. emails, SMSs and Twitter). Various APIs and collected data from sensors can be the source for alert emails. 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.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

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.


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 light weight 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:

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

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."

To send a test email use:

sudo echo "Test email" | mail -s "Testing ssmtp setup"

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

Tips & tricks

Because the entire email is send 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/

Add the following to it:

# Bash sample code to help with string paragraph and some special character layout


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"

Note the -e after the echo.

Test the email script:

sudo /home/pi/

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 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:


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."

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. Also see Using Cron for scheduling tasks for more information…

Related product links

Please support Behind The Scenes by using these links to purchase these related products online from our partner websites. Links will open in a new window...

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.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More website development related posts

Get WP Toolset – for custom WordPress development without programming
Get WP Toolset – for custom WordPress development without programming
18 August 2019
Ad: Extend the functionality of WordPress with regards to its ability to store data and publish it on your website. More…
Using Cron to schedule tasks
Using Cron for scheduling tasks
30 June 2017 | Updated 11 July 2017
Cron is an Unix-like operating system software utility used to schedule automatic time-based commands. Entered commands can be in the form of Bash or Bash scripts that are meant to be automatically executed periodically at fixed times, dates or intervals. More…