Table of Contents
If you stumbled upon this post it means that you want to find a way to send e-mails from your Linux server and you're looking for a simpler alternative to configure than Sendmail and Postfix mail services. This is a rather common need for those who run simple sites / blog / services in a LAMP (Linux, Apache, MySQL, PHP) environment and need to set the value of the sendmail_path property in the php.ini file.
The alternative in question is called sSMTP and is a minimalistic Mail Transfer Agent (MTA) which can be used to send outgoing mail from your Linux system. That said, the choice is yours: if you still prefer to install the real deal, read how to install Postfix on CentOS; otherwise, keep reading.
Setup
Let's see how to install sSMTP and use it in the correct way with the main SMTP e-mail service providers used in Italy, that is GMail and Aruba. The steps shown in this post are valid for a RedHat / CentOS 7 / Fedora distribution, but can be also used on any other Linux distribution without problems.
The first thing we have to do is to install the ssmtp package within our Linux server, by executing the following Terminal command - depending on your Linux distribution:
RedHat, CentOS7, Fedora
1 |
yum install ssmtp |
If you receive a "Package ssmtp is not available" error, you'll need to install EPEL on your machine with the following command:
1 |
yum --enablerepo=extras install epel-release |
Once done, you'll be able to install ssmtp using the above command.
Ubuntu, Debian
1 |
sudo apt-get install ssmtp |
Configuration
Once we've got it installed, sSMTP will create his two configuration files in the /etc/ssmtp/ folder:
1 2 |
/etc/ssmtp/ssmtp.conf /etc/ssmtp/revaliases |
The first file, which we need to setup properly, is meant to contain the connection info to the remove SMTP server: the second file, which is rather optional, allows to setup some aliases for the local users: these aliases, if present, will replace the SMTP parameters specified in the first file.
Both files are well-documented by the inline comments and rather simple to configure: in the following paragraphs we'll see some examples to configure the service for some SMTP providers such a GMail, Aruba and Yahoo.
GMail
Here's a /etc/ssmtp/ssmtp.conf sample file that we can use to configure sSMTP with the [email protected] GMail account:
1 2 3 4 5 6 7 |
root=gmail@gmail.com mailhub=smtp.gmail.com:587 AuthUser=gmail_username AuthPass=gmail_password AuthMethod=LOGIN UseSTARTTLS=YES UseTLS=YES |
If we also want to configure the /etc/ssmtp/revaliases file, here's how we can give a valid alias to the root user:
1 |
root:gmail@gmail.com:smtp.gmail.com:587 |
Aruba
Here's a /etc/ssmtp/ssmtp.conf sample file that we can use to configure sSMTP with the [email protected] account, for SSL and non-SSL mail delivery servers provided by Aruba:
SSL (TCP port 465)
1 2 3 4 5 6 |
root=aruba_email@mysite.com mailhub=smtps.aruba.it:465 AuthUser=aruba_email@mysite.com AuthPass=aruba_password AuthMethod=LOGIN UseTLS=YES |
NON-SSL (TCP port 25)
1 2 3 4 5 6 |
root=aruba_email@mysite.com mailhub=smtp.mysite.com:25 AuthUser=aruba_email@mysite.com AuthPass=aruba_password AuthMethod=LOGIN UseTLS=NO |
And here's the corresponding /etc/ssmtp/revaliases content in case we want to configure an alias for root:
1 |
root:aruba_email@mysite.com:smtps.aruba.it:465 |
1 |
root:aruba_email@mysite.com:smtp.mysite.com:25 |
Yahoo.it
File /etc/ssmtp/ssmtp.conf :
1 2 3 4 5 6 7 |
root=yahoo_email@yahoo.it mailhub=smtp.mail.yahoo.it:587 AuthUser=yahoo_username AuthPass=yahoo_password AuthMethod=LOGIN UseTLS=YES UseSTARTTLS=YES |
File /etc/ssmtp/revaliases :
1 |
root:yahoo_email@yahoo.it:smtp.mail.yahoo.it:587 |
Test run
Once done, it is advisable to perform a rapid test to confirm that everything is properly working. The easiest way to check that is to run the ssmtp command within a terminal shell: to do so, we can either use interactive mode - writing the message immediately before sending it - or send a previously prepared message saved in a text file.
Let's see them both:
Interactive mode
1 |
> ssmtp delivery@address.com |
Immediately after executing the above command, we'll get the chance to write our message in the following way:
1 2 3 4 |
To: delivery@address.com Subject: sSMTP test ... it works! |
We need to pay attention to leave an empty line between the Subject and the body of the message, otherwise the e-mail message could have issues. Also, it's important to keep in mind that the To: field is purely indicative, as the message will only be sent to the recipient specified in the command line above.
Send a file
We can also send a previously prepared message saved within a standard text file. The syntax of the text file is the same we've just seen for the Interactive mode, i.e.:
1 2 3 4 |
To: delivery@address.com Subject: sSMTP test ... it works! |
After we wrote the following text within a /email/msg.txt file using a text-editor such as nano or vim, we'll be able to send it using the following terminal command:
1 |
> ssmtp delivery@address.com < /email/msg.txt |
Using sSMTP with PHP
sSMTP can be also used as a drop-in replacement of sendmail to send mail through PHP using the native PHP mail() function: all we need to do is to configure the sendmail_path parameter within our php.ini file in the following way:
1 |
sendmail_path = /usr/sbin/ssmtp -t |
And that's it!
Useful links
Here's a couple links that will surely be useful for anyone who would like to further deepen the topic:
- https://packages.debian.org/source/sid/ssmtp : sSMTP official repository.
- http://guide.debianizzati.org/index.php/Ssmtp : full sSMTP guide (in italian) made by (and hosted upon) the debianizzati.org web site.
That's it for now: I sincerely hope that this guide will help those who want to setup a simple e-mail sending service to their Linux environment!
On debian buster :
matt@TP430:/ $ sudo apt install ssmtp
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package ssmtp is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
??
Hello there!
You’ll need EPEL installed and enabled to get ssmtp.
Just perform the following command:
yum –enablerepo=extras install epel-release
.. and then you can use:
yum install ssmtp
Updated the post accordingly! If you manage to fix that, please give us a like on FB/Twitter :)