Install sSMTP on Ubuntu to Send Email Using External Server

sSMTP is a program which help deliver and email from a linux machine via external email server using SMTP protocol. The program is useful for a machine or server which does not have email server but need to send out an email, such as system notification email.

Installing sSMTP is easy and straightforward by using the command

sudo apt-get install ssmtp -y

Configure SMTP server

After installing, in order to send an email, we’ll need to configure ssmtp.conf file as follows

sudo vim /etc/ssmtp/ssmtp.conf

Set SMTP to the destination server. Example configuration for sending through Gmail

# Sender email address
root=username@gmail.com

# Destination SMTP server and port
mailhub=smtp.gmail.com:587

# Username and password
AuthUser=username@gmail.com
AuthPass=TYPE-PASSWORD

# Sender domain
rewriteDomain=gmail.com

# Machine's hostname
hostname=localhost

# Allow set From name in each email
FromLineOverride=YES

Config revaliases

Revaliases is use to map local user to form address in outgoing mail. We can config it by edit revaliases config file

sudo vim /etc/ssmtp/revaliases

And add the following line

root:username@gmail.com:smtp.gmail.com:587

Test sending email

When all the settings are done. We’ll be able to test sending e-mail by using ssmtp command follow by recipient email address:

ssmtp mail_to@domain.com

Then we’ll get a blank line, type:

To: mail_to@domain.com
From: username@gmail.com
Subject: This is test email

This is test message

When done, press ctrl + d, wait a moment, the system will send a test e-mail to the recipient e-mail.

If all setup is complete, sSMTP will become your default e-mail program for the machine. If other program want to send an email such as notifications. They will be sent through this sSMTP program.