RPi email using sSMTP
Introduction – There has got to be an easier way.
After setting up Cacti and automating tasks with CRON and Webmin, email notification was the next step. Searches for sending email from Linux typically generate results that gravitate around sendmail. This is no different for the RPi. Sendmail has its good points, but its a service that runs constantly and provides features well beyond simple email notification.
I wanted a binary that just sends emails and only runs when I need it. This was the reason for sSMTP.
Purpose – When a guide isn’t all that clear
My initial findings looked promising. All I had to do was follow a few steps; install, config, and run. The problem I ran into was the config portion. None of the well formatted posts I viewed gave the correct steps and this led to my trouble. So as a result, I decided to take time during my Winter break and do this write up.
Details – When you want it done right…
The first step is to open a command shell on the RPi and check for updates.
sudo apt-get update
If you have any install them first, reboot as needed.
sudo apt-get upgrade
Next up, you’ll want to install the sSMTP binaries, super easy so far.
sudo apt-get install ssmtp
Now that this is done, open the config file and edit it for your system.
sudo nano /etc/ssmtp/ssmtp.conf
This will open up the file and give you areas to edit so they work with your email host. The system below uses encryption in the authentication, so the mailhub has a port number (587) tagged on the end with a colon (:).
==========(SOF)=================================================
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=********@yourdomain.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=**************.yourdomain:587
AuthMethod=LOGIN
AuthUser=********@yourdomain.com
AuthPass=**********
UseSTARTTLS=YES
# Where will the mail seem to come from?
rewriteDomain=
# The full hostname
hostname=rpi.yourdomain.com
# 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
=================================================(EOF)==========
With the proper formatting out of the way, you should be able to run a test script using a text file as your message header and body. Create a text file in nano and enter in the info that pertains to you.
==========(SOF)=================================================
To: ********@yourdomain.com
From: ********@yourdomain.com
Subject: Scripted Message
This is a scripted message to check if ssmtp will work from the RPi.
=================================================(EOF)==========
Now with the text file all set, you can run the script from the command prompt.
ssmtp ********@yourdomain.com </home/user/msg.txt
After a few moments, you should or someone else, will have the email message in the inbox. If you have trouble, Webmin is here to help. In the System section, click System Logs and choose the /var/log/mail.log log to view. This helped me narrow down why the earlier posts didn’t cut it for me. I’m not without fault, so this might help you find why I didn’t cut it.
Relations – Automated notification.
Now that the business of emailing from RPi is out of the way, you can go about setting up notifications. These can be scheduled with CRON scripts, or scripts that are called by other processes, such as Cacti. Cacti has some marvelous plugins that would be drab without the email function. This step is key to allowing that to work.
Summary – Now you get the message.
The task of email notification should be kept as simple as possible when dealing with the RPi. For that, I showed how to install, setup, and use sSMTP as that notification tool. The setup requires attention to the proper entries in the config file. I also covered how to debug, should any unexpected problems arise. Once the initial setup and config work was done, I then stepped through how to send a message from the command line. We then went into more details of potential use for sSMTP with Cacti.
The sites that I had found useful are listed here; these were instrumental in my success with sSMTP:
https://wiki.debian.org/sSMTP
http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
http://forums.gentoo.org/viewtopic-t-439502-start-0.html