How to send emails from a script without MTA using sSMTP

In the past I used sendEmail from Brandon Zehm, but unfortunately it won’t work (for me) on Debian 8. It ssems that there are problems with SSL (I received Perl errors about unsupported ciphers…).

So I searched around and found the Debian package ssmtp which works fine without an big and hungry MTA like postfix or Exim.

Prerequisites

To get sSMTP installed it an absolutely requirement that name resolution of your own hostname is working on your system. Otherwise the package throws errors and will not be installed correct.

To check your hostname type:

hostname -f
hostname --fqdn

If receive an error your hostname is not set correctly.

To setup an correct hostname use the editor of your choice and modify the file /etc/hosts:

sudo nano /etc/hosts

Modify the first two lines from:

127.0.0.1 localhost
127.0.0.1 server

to:

127.0.0.1 localhost.localdomain localhost
192.168.xxx.xxx server.example.com server

Restart your system.

Install

After your system has been restarted you shouln’t receive errors using the hostname command. Now we can install sSMTP:

sudo apt-get install ssmtp

A few seconds later sSMTP is installed and we can start to configure it.

Configuring

We have to modify two different confoguration files. The first one is /etc/ssmtp/ssmtp.conf which will held the global configuration for sSMTP. The second one is /etc/ssmtp/revaliases where you can define who is able to send emails using sSMTP.

sudo nano /etc/ssmtp/ssmtp.conf

Modify the file with your needs. I will use GMail to send emails.

root=<Your Name>@gmail.com
mailhub=smtp.gmail.com:587 # GMail supports only chiphered and authenticated sending of emails
AuthUser=<Your Email Address>
AuthPass=<Your Password>
UseSTARTTLS=YES

Now we will modify the second configuration file to define which account can send emails

sudo nano /etc/ssmtp/revaliases

and add the root user (which should be the only one who can send emails from my system)

root:<Your Name>@gmail.com:smtp.gmail.com:587

Now we are finished with our configuration and can start using sSMTP.

Use

to be continued…

Go to Top