How to send email alerts from Ubuntu Server using ssmtp
Once you’ve got your server up and running the last thing you want to be doing is logging onto it every 5 minutes to check everything is ok. That said, some people actually enjoy doing this and if you’re one of those people then you can skip this page completely. For the rest of you, read on!
If you’ve been following this guide from the start you’ll know I’ve configured my server to send me an email if either the CPU or any of the hard drives exceed my pre-determined temperatures. I also get emails when any of my Torrents have downloaded. I also get emails to tell me about the state of my SnapRAID Array. Infact you can get email alerts on pretty much anything you desire!
Whilst you can install and configure a fully featured email system you really don’t need to do so if all you want to do is send emails and not receive them too. I use ssmtp which is a simple Mail Transfer Agent (MTA). It’s not rocket science to install ssmtp, it’s one simple command, although configuring it can be a bit more challenging. So, how do you install it?
How to install ssmtp
Before we do anything else we should bring the Ubuntu Repository up to date. So, from a Putty Session or, if you’ve got a screen and keyboard attached to your server, then you can use the command line itself to type the following:
sudo apt-get update
You’ll be prompted for a password. This is the password you created when you installed Ubuntu. Ubuntu tends to prompt for a password each time you issue a “sudo” command.
Next we can install the ssmtp package:
sudo apt-get install ssmtp
How to configure ssmtp
Now we’ve installed the package we need to configure it. I’m using my gmail account to handle all my emails but you can obviously use your favourite email address. Obviously you should alter the following to text suit your own setup:
Firstly, we’ll configure the config file. I like to use vim which is a powerful but simple text editor (issue: sudo apt-get install vim if you don’t have it installed already). So, let’s edit the config file
sudo vim /etc/ssmtp/ssmtp.conf
This will open the config file for editing. Then press the [Insert] key once to switch into Insert Mode and edit the file as follows. I mask out the original lines using a # but you can simply edit them instead:
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
# root=postmaster
root=MyEmailAddress@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=mail
mailhub=smtp.gmail.com:587
AuthUser=MyEmailAddress@gmail.com
AuthPass=MyPassword
UseTLS=YES
UseSTARTTLS=YES
# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com
# The full hostname
#hostname=MyMediaServer.home
hostname=MyEmailAddress@gmail.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
Once you’ve finished editing the file press the [Esc] key once and type :wq to save and quit out of the file. If you make a mistake editing the file then issue :q! instead of :wq to abort your changes.
Adding reverse aliases
A reverse alias changes the “From” address. This means you can make the email appear as if it’s from a different email address. I personally haven’t done this but if it’s something you’d like to do then edit the revaliases file as follows:
sudo vim /etc/ssmtp/revaliases
Then add a new line similar to this:
root:YourFromName@gmail.com:smtp.gmail.com:587
How to test you have configured ssmtp properly
Once you’ve configurd sSMTP it’s time to try and send an email. The simplest way to do this is to run sSMTP in a terminal with a recipient email address. So:
ssmtp recipient_email@example.com
sSMTP will then wait for you to type your message, which needs to be formatted like this:
To: recipient_email@example.com
From: myemailaddress@gmail.com
Subject: test email
Hello World!
Note the blank like after the subject field. Everything you type from the Hello World! onwards is the body of the email. Once you have finished composing your email hit Ctrl-D. After a few seconds sSMTP will send the message.
Obviously you don’t want to be doing stuff from the Command Line each time you want to send an email so it’s better to write a little text file containing the email contents. Here is such a text file for use in the script which monitors CPU Temperatures:
To: myemailaddress@gmail.com
From: myemailaddress@gmail.com
Subject: alert
The critical CPU temperature has been reached. The server is shutting itself down!
If you look at the script itself you can see it’s being called on the following line:
/usr/sbin/ssmtp myemail@myaddress.com </home/htkh/MyScripts/msg.txt
Once you have configured sSMTP it becomes the default “email client” and so you’ll start receiving relevant output from your Cron jobs too.