Sunday, June 7, 2009

How to send e-mail using GMail on CentOS from shell script

sSMTP is a very simple and straightforward alternative to big MTAs like sendmail or Exim. Unfortunately CentOS repositories don’t come with it, so you have to fetch it from Fedora’s EPEL repo.

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
yum install ssmtp


Configuration is rather simple. Just change the following values in your /etc/ssmtp/ssmtp.conf:


root=noreply@youdomain.com
AuthUser=noreply@yourdomain.com
AuthPass=password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

To test just run a following command("-v" should be used for debugging):

echo "test" | ssmtp -v -s "Test" genadyg@exelate.com


To use sSmtp in unix script:

ssmtp myemailaddress@gmail.com < msg.txt

msg.txt is a simple text using the proper formatting for sSMTP:

To: toaddress@gmail.com
From: fromaddress@gmail.com
Subject: alert
Message text

No comments: