sSMTP is a simple MTA (mail transfer agent) to deliver mail from a computer to a mail hub (SMTP server). sSMTP is simple and lightweight, there are no daemons or anything hogging up CPU; Just sSMTP. Unlike Exim4, sSMTP does not receive mail, expand aliases, or manage a queue.


How to: Use sSMTP To Send Outbound Email


You can use sSMTP to send emails directly from the command line. sSMTP requires an SMTP mail server to function. For this example we'll be making use of Gmail’s SMTP server.


Installing sSMTP

1. sSMTP is available to install from the built in APT package manager. First we will update the packages and then install sSMTP.

apt-get update
apt-get install ssmtp


Configuring sSMTP

1. You can use your favorite text editor to configure the ssmtp.conf file. In this case we are using nano.

nano /etc/ssmtp/ssmtp.conf


2. In order to use Gmail’s SMTP server configure sSMTP with the following parameters.

# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=EmailAddress>@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


AuthMethod=LOGIN
AuthUser=EmailAddress@gmail.com
AuthPass=EmailPassword
UseTLS=YES
UseSTARTTLS=YES


# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com


# The full hostname
#hostname=MyMediaServer.home
hostname=hostname


# 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


Note: If you do not know your VPS hostname you can print it to the terminal by running

hostname


Testing sSMTP

You can test if sSMTP is working running the following command

ssmtp yourEmail@example.com


This will start the sSMTP process you can format the email as follows

To:yourEmail@example.com
From:EmailAddress@gmail.com
Subject:This is an sSMTP Test Email
I am testing sSMTP.

Press Ctrl-D when you are finished writing the body and sSMTP will send the Email.


Additional Security Steps

For security purposes it is a good idea to create another user to send mail with if other people have access to the computer. Your Gmail password is left unencrypted in the ssmtp.conf file!


To create a new user run the adduser command

adduser username

Make sure you have a strong root password and hardened network security settings.


1. Changing file permissions

chown root:mail /etc/ssmtp/ssmtp.conf
chmod 640 /etc/ssmtp/ssmtp.conf


2. Add the user to the mail group

usermod -a -G mail username


3. Log out of all sessions and log back in to test the changes

ssmtp yourEmail@example.com


To:yourEmail@example.com
From:EmailAddress@gmail.com
Subject:This is an sSMTP Test Email
I am testing sSMTP!


Changing Google App Settings

If your message does not send it may be filtered by Google’s secure App settings. You can disable these at the following link.

https://myaccount.google.com/lesssecureapps


Sending Predefined .txt Emails

You can send predefined emails from .txt files using the following format and command:


1. Create an example.txt file with a text editor

nano example.txt


To:yourEmail@example.com
From:EmailAddress@gmail.com
Subject:This is a test
Hello this is a test sSMTP email.


2. Send the contents of the example.txt file by running.

ssmtp yourEmail@example.com < example.txt


You now know how to send emails from the command line using sSMTP!


For additional resources on specifics of using sSMTP, please consult sSMTP Support and Community reference material:

sSMTP Debian Wiki

sSMTP Man Pages