Thursday, May 15, 2014

How to send email alerts from Ubuntu Server using ssmtp


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:

0 comments: