To totally unlock this section you need to Log-in
Login
Email notification are usually used in almost every environment to check if your brand new server (or multiple servers, even thousands) are up and running without the need to log onto it every 5 minutes to check everything is ok.
If you have opened this article and following it, you will probably searching for a efficient method to send yourself, for example, an email if either the CPU or any of the hard drives exceed your pre-determined temperatures. You could also get emails when your scheduled torrents files have been downloaded or, again, you could also get emails to send important info the status of your home RAID array. With this approach you could get emails from almost any controllable system of device connected to you monitoring Linux server.
For normal and advanced notifications you can surely install and configure a fully featured email system/server, but it not necessary at all if all you want to do is send emails and not receiving them.
In this article we could use ssmtp, which is a simple command line Mail Transfer Agent (MTA). It's not so difficult o install ssmtp on a Linux system, it's one simple command, but configuring it could be a bit confusing and challenging. Let's see now how to install it.
Installing ssmtp
Taking Ubuntu a a reference Linux system, first we need to bring the Ubuntu Repository up to date. Opening an normal SSH session with Putty, or loggin on the system with a screen and a keyboard attached to the system, you will need to run and type the following simple command (the system needs to be correctly connected to Internet):
sudo apt-get update
You'll be prompted for an administrative password (if you are not logged in as root). This is usually the password you created at Ubuntu first installation phase. Ubuntu will prompt to you for a password each time you will issue a "sudo" command (if you are not an administrative account).
Once terminated the repository update, we can proceed by installing the ssmtp package:
sudo apt-get install ssmtp
Configuring ssmtp
Now that we have installed the package we need to configure it. We will use a gmail account to handle outgoing e-mails but does not deny the usage of other public and free email services. Obviously you will have to search and gain the info needed to follow the proposed setup based on Gmail.
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 command will open the config file for editing. Once opened, press the [Insert] key once (on the keyboard) to enable the Insert Mode and edit the file as follows: we will 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 [email protected]
# 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
AuthMethod=LOGIN [email protected] AuthPass=MyPassword UseTLS=YES UseSTARTTLS=YES
# Where will the mail seem to come from? #rewriteDomain= rewriteDomain=gmail.com
# The full hostname #hostname=MyMediaServer.home [email protected]
# 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 modified the file you will need only to press the [Esc] key once and type :wq to save and quit out of the file. If you make a mistake editing the file you can always issue the :q! command instead of :wq to abort your dangerous changes.
Adding reverse aliases
A reverse alias is needed when you wanto to change the "From" address. This means you can make the email appear as if it's from a different email address. If you are searching for something like this you can edit the revaliases file as follows:
sudo vim /etc/ssmtp/revaliases
Then add a new line similar to this:
root:[email protected]:smtp.gmail.com:587
Testing SSMTP
Once you've configured sSMTP it's time to try and send a test email. The simplest way to do this is to run sSMTP in a terminal with a recipient email address. So:
ssmtp [email protected]
After you will press Enter key sSMTP will ask and wait for you to type your message, which needs to be formatted like the following way:
To: [email protected] From: [email protected] Subject: test email
Hello World!
Note the blank like after the Subject line. Everything you type from the Hello World! onwards will be considered the body of the e-mail. Once you are satisfied and your message is finished, just hit Ctrl-D key combination to inform sSMTP that the e-mail is completed and can be sent. After a few seconds sSMTP will send the message using the pre-configured connector (in ssmtp.conf file).
Obviously you don't want to be doing stuff from the command line and terminal each time you want to send an e-mail so it's better to write a little text file containing all the needed email contents. Here is such an example text file to use in the script which monitors CPU temperatures:
To: [email protected] From: [email protected] 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 [email protected]
Once you have configured sSMTP it becomes the default "e-mail client" and so you'll start receiving relevant output from your Cron jobs too. Take note that you could even create specific text file to send to sSMTP by using more or less complex bash script to include dynamic content to the text file that will be used by sSMTP to craft the email.