Geoff Garbers

Husband. Programmer. Tinkerer.

Sending mail through a remote SMTP server in Linux

Dec 05, 2010

Those who are like me, and don’t have sysadmin-like knowledge of the Linux command line and servers would undoubtedly have come across the issue of having a server set up, but not having mail set up on it.

If this is a home-grown server, or you find yourself needing to send mail through PHP (as I did on a recent project), it is a daunting task to set up a mail server. However, if you’re in the fortunate position to have an external SMTP server available to you, this is the post for you!

I was searching around for ways of replacing the default mail application available through the Linux command line, when I stumbled on this gem of an application, named ssmtp. It makes it ridiculously easy to send email through a remote SMTP server using either the command line variant of mail, or (once set up as the default) through PHP’s mail() function. I’ll walk you through the steps required to set it up:

1. Retrieving the SSMTP package

We’ll need to first install the ssmtp package. If you’re on Ubuntu or Debian (these are the only two Linux distributions I’ve tested this on), it’s really as simple as:

sudo apt-get install ssmtp

2. Configuring SSMTP

The next step involves setting up the configuration for ssmtp. The configuration file is located at /etc/ssmtp/ssmtp.conf. Before editing this file, make a backup of this configuration file, and then edit the original.

This is how my default configuration file looked before editing it:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# 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

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

# The full hostname
hostname=geoff-laptop

# 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

And this is how it looks after I’ve customized it to use my required SSMTP server:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=geoff@garbers.co.za

# 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=xx.xxx.xx.xx:25

# Where will the mail seem to come from?
rewriteDomain=garbers.co.za

# The full hostname
hostname=garbers.co.za

# 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

It really is as simple as that. Should you want to know what other configuration options are available to you, the details for the Debian version of the package is available at http://wiki.debian.org/sSMTP.

3. Sending mail

Straight off the bat, you’ll notice that the method for sending mail from SSMTP is a little bit different to the way you send mail from mail. First of all, you need to initialize the message with the recipient’s email address:

ssmtp recipient@hostname.com

Then, SSMTP will be waiting for your input. The input that follows will need to be supplied in the following format:

To: recipient@hostname.com
From: yourname@yourhost.com
Subject: This was sent using SSMTP.

This email was sent using the nifty command-line mailing tool, SSMTP.

Note the extra line break between the Subject field and the mail body. Everything after the subject is considered to be part of the mail’s body. Once you’re finished composing your email, you can press CTRL-D to tell SSMTP you’re finished composing your mail. It might seem to hang a little bit just after pressing it - this is alright. This is just SSMTP performing the actual sending of the mail.

Should you wish to use SSMTP as your default command line mail application, the following should be sufficient to set this up:

sudo unlink /usr/bin/mail
sudo ln -s /usr/sbin/ssmtp /usr/bin/mail

Be sure to remember where the original mail application lies. Should you wish to revert back, you’ll need to perform the linking/unlinking again, just in reverse.

If you’ve checked the recipient’s email, and you’ve managed to send the email successfully, congratulations! If this is the first time you’ve used SSMTP, you’ll soon come to realize just how awesome this tool is. If not, I hope you’ve found it as useful as I have.

If you haven’t been able to send the mail successfully, hit me up in the comments, and I’ll try help you out where I can.