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.
To the the software installed, do this:
sudo apt-get update sudo apt-get install ssmtp sudo apt-get install heirloom-mailx
Edit the /etc/ssmtp/ssmtp.conf file:
# The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=<yourGMailId>@gmail.com # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587 # Where will the mail seem to come from? rewriteDomain=gmail.com # The full hostname hostname=<yourRPIHostName> # 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 FromLineOverride=YES # Use SSL/TLS before starting negotiation UseTLS=Yes UseSTARTTLS=Yes # Username/Password AuthUser=<yourGMailId>@gmail.com AuthPass=<yourGMailPassword>
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 18.104.22.168.54384 > 22.214.171.124.587: tcp 0 13:23:17.549450 IP 126.96.36.199.587 > 188.8.131.52.54384: tcp 0 13:23:17.549776 IP 184.108.40.206.54384 > 220.127.116.11.587: tcp 0 13:23:17.576155 IP 18.104.22.168.587 > 22.214.171.124.54384: tcp 52 13:23:17.576929 IP 126.96.36.199.54384 > 188.8.131.52.587: tcp 0 13:23:17.577777 IP 184.108.40.206.54384 > 220.127.116.11.587: tcp 13 13:23:17.604982 IP 18.104.22.168.587 > 22.214.171.124.54384: tcp 0 13:23:17.609090 IP 126.96.36.199.587 > 188.8.131.52.54384: tcp 167 13:23:17.609863 IP 184.108.40.206.54384 > 220.127.116.11.587: tcp 10 13:23:17.633297 IP 18.104.22.168.587 > 22.214.171.124.54384: tcp 30 13:23:17.636871 IP 126.96.36.199.54384 > 188.8.131.52.587: 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:
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:
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.