Tuesday, 17 September 2013

HOWTO: Send mail via a mailhub in CentOS

To do this I use ssmtp which is far easier to configure for a simple task than sendmail or even postfix. If you are intending to build a full mail server then this is not the correct way to do so, I only want my CentOS server to be able to send out mail alerts, not act as a full on mail hub.

So, let's get down to it, I will be starting from a "minimal" install of CentOS 6. You must also ensure that your host has a FQDN on your network otherwise your mail hub will refuse to relay any emails from it.

Firstly, as is often the case the standard CentOS repos are a lot more sparse than those in Debian/Ubuntu land so we need to add a Fedora repository:

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Note: The above is for CentOS 6 x64, if you are using CentOS 5 or a 32 bit OS then modify the above line accordingly. For example, for CentOS 5/32 you would browse to this URL: http://download.fedoraproject.org/pub/epel/5/i386
and then find the epel package at that address e.g. epel-release-5-4.noarch.rpm

We will need to remove postfix

# yum remove postfix

Now install SSMTP and mailx

# yum install ssmtp mailx

Backup the default ssmtp config file

# cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.default
# vi /etc/ssmtp/ssmtp.conf

Find the mailhub=mail line and change it to the hostname of your main mail server:

e.g. mailhub=mail.tuxnetworks.com

You can now send a test email:

mail -s TEST me@mydomain.com
(type in some text and press ctrl+d when done)

You can check how things went in your mail log:

# tail -f /var/log/maillog

If all went well you should see something like this:

Sep 17 09:19:20 myhost sSMTP[31558]: Sent mail for root@myhost.tuxnetworks.com (221 2.0.0 Service closing transmission channel) uid=0 username=root outbytes=510

If you see that and your email arrives then congratulations, you are done.

No comments: