Raspberry Pi: Sending Email Status Information

Ever since nearly the dawn of email, I have been sending status information via email. If a script encounters a nasty problem, I need to know. And I’d rather NOT know the rest of the time. When an exception occurs in a script, I send myself (or more likely operations) an email.

In the past, none of my Raspberry Pi systems have been doing anything requiring monitoring but that has changed with the weather monitoring (WVIEW) Rasperry Pi. I do need a status message from it when the WX station hangs.

Before I can send myself email status information, I need to get email up and running on Raspberry Pi. I implemented SENDMAIL way back when and it was not quick and dirty. I really don’t want to spend hours trying to get it running. Nor spending time trying to debug it 12 months down the road.

So, instead, I will use SSMTP on the Raspberry Pi. SSMTP simply sends mail from a public message server (GMAIL in my case). This is perfect for the RPI.

My primary source for installing SSMTP is the ArchLinux SSMTP Wiki Page. I will very briefly summarize what I do. For a better explanation or more detailed troubleshooting, refer back to that page.

Install Software

To the the software installed, do this:

sudo apt-get update
sudo apt-get install ssmtp
sudo apt-get install heirloom-mailx

Configure SSMTP

Edit the /etc/ssmtp/ssmtp.conf file:

# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com

# Where will the mail seem to come from?

# The full hostname

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address

# Use SSL/TLS before starting negotiation

# Username/Password

Type in the following commands to secure the config file and allow root and pi users to use mail:

chmod 640 /etc/ssmtp/ssmtp.conf
chown root:mail /etc/ssmtp/ssmtp.conf
gpasswd -a root mail
gpasswd -a pi mail


You should be good to go, now test it by sending yourself an email from the RPI’s command line:

echo test | mail -s "Testing SSMTP Setup" <yourEmailAddress>


Did you receive the email? If not, check the troubleshooting section of the documentation above.

To use tcpdump to monitor the connection between the RPI and GMail, type the following:

apt-get install tcpdump
tcpdump -i eth0  port 587 -nn -q

This will print a line for every packet sent and received. You should see something like this:

13:23:17.524736 IP > tcp 0
13:23:17.549450 IP > tcp 0
13:23:17.549776 IP > tcp 0
13:23:17.576155 IP > tcp 52
13:23:17.576929 IP > tcp 0
13:23:17.577777 IP > tcp 13
13:23:17.604982 IP > tcp 0
13:23:17.609090 IP > tcp 167
13:23:17.609863 IP > tcp 10
13:23:17.633297 IP > tcp 30
13:23:17.636871 IP > tcp 89

If you are seeing only a couple of packets, something is definitely wrong. Maybe a wrong password or something is blocking packets.

Networking troubleshooting is beyond this document; however, you can take a look here for more information:

A tcpdump Tutorial and Primer

Configuring mailx

My first iteration was to install mail from the mailutils package. It turns out that version of mail had some omissions I really wanted. Like sending attachments and having a default from address so I could change from to be root.<host> rather than just root.

I ended up uninstalling mailutils and installed heirloom-mailx instead of which this procedure is now based. The mail in heirloom-mailx is very feature rich. It appears one could actually dispense with ssmtp and configure mailx to transmit to gmail. However, ssmtp is easy to setup so I will only reconfigure mailx to the extent of supplying a decent from address.

For my own scripts, I have access to the shell and I want to be able to know which ‘process’ sent the email. so I indicate this with the -r flag in the mail command (the from address will be reported to the receiver as ‘wxdude’):

echo test | mail -r "myEmailAddr@gmail.com(wxdude)" \
-s "test message" myEmailAddr@gmail.com

Once I had mail implemented, I started getting status messsages from root. This is because in the /etc/ssmtp/ssmtp.conf file I assigned ‘root=<myemailAddr>’.

I kind of like these messages (at least for the moment), but having a bunch of RPI’s I need to know which actually sent it since they all are sharing one gmail account.

To identify the sender uniquely, I create a .mailrc file in the /root directory like this:

set from=myAddr@gmail.com(root.wxdude)

I could do the same for the pi user or my own user as well, and then wouldn’t need to specify the -r in my scripts.

I’ll be using this in a script I’ll document shortly to let me know when my weather station hangs.


This entry was posted in c-rpi and tagged . Bookmark the permalink.

3 Responses to Raspberry Pi: Sending Email Status Information

  1. Pingback: Recovering from WVIEW’s WH1080 “Bad Magic Number” on Raspberry Pi | Big Dan the Blogging Man

  2. Pingback: My Minimal Configuration for a New Raspberry Pi | Big Dan the Blogging Man

  3. Ben says:

    Thanks for this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.