Tuesday, October 2, 2012

Send email from command line through an MS Exchange Relay (Alt title: Installing SSMTP on CentOS 5.x)

The situation required me to use an existing MS Exchange Server as a mail relay to send an email from a custom notification shell script I'm working on.

I tried configuring Postfix, Sendmail, Mailx, Nail, all to no avail. This took quite some time in troubleshooting and Google was not particularly helpful.

Eventually I stumbled across this post, which mentioned using the EPEL repo's to install SSMTP. Simple, but effective.

Therefore, this entry focuses on how to setup your server to relay mail, using the SSMTP package (because it works)


0. Login as root or su.

1. Check to ensure that the permissions are correct to relay mail.
telnet my.emailserver.ip.address 25
EHLO
MAIL FROM: myvalidsenderaddress@foobar.org
RCPT TO: myvalidrecipient@foobar.org
DATA
This is a test message.
. <Hit Enter>


quit

You should have received an email if the permissions are correct.

2. Install the EPEL repo

cd /tmp
wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

 Please use this alternative if the original does not work. - http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

rpm -Uvh epel-release-5-4.noarch.rpm

3. Remove sendmail
yum remove sendmail

4. Install SSMTP
yum install ssmtp --enablerepo=EPEL

5. Configuration
Backup original conf file
cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bkp

Edit the conf file
nano /etc/ssmtp/ssmtp.conf 

Edit the following lines:
mailhub=my.emailserver.ip.address:25
Hostname=ValidDomainHostName.foobar.org

*Depending on your configuration, you may or may not need TLS, check the SSMTP.conf man for details.

6. Restart service
service ssmtp restart

7. Test the application
echo "This is the body of my email" | ssmtp myemail@foobar.org

That's it!

This worked for me where all the others mysteriously failed. Now I'm off to finish my script. I'll post another update soon!

-noveck