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.
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 (
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
Make a backup of the original
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
and add the following lines (with your own details):
AuthUseremail@example.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." firstname.lastname@example.org
To send a test email use:
sudo echo "Test email" | mail -s "Testing ssmtp setup" email@example.com
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/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" firstname.lastname@example.org
-e after the echo.
Test the email script:
Using sSMTP and Cron
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:
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
* * * * * command | mail -s "Subject text here." email@example.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:
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…