Tuesday, August 04, 2009

Sending emails from server without a local smtp server

First of all, I do not like to have unnecessary daemons running on a server that are not related to the server's function and the excuse that it is easier that way does not convince me. So here is a cookbook recipe to send emails from a server without using the local smtp server (very useful for CRON scripts and other maintenance scripts).

1. You need to install mailx:

#---
yum -y install \
mailx
#---


NOTE.: For CentOS you will need nail instead of mailx (they crippled mailx in CentOS):

1.1. Install the repository from http://centos.karan.org/:

#---
wget http://centos.karan.org/kbsingh-CentOS-Extras.repo -O /etc/yum.repos.d/kbsingh-CentOS-Extras.repo
#---


1.2. Install nail:

#---
yum --enablerepo=kbs-CentOS-Testing -y install \
nail
#---


2. You need a copy of your SSL root certificates in the server.

2.1. On your client box, transfer your SSL certificates to the server:

#---
scp $HOME/.mozilla/firefox/<something>.default/cert<a number>.db <server ssh user>@<your server>:/path/you/can/write
#---


2.2. Go to the server and put the certificate db at some path your script has access to

3. Create a GMail account, that will be the sender in your scripts (the password will be stored on the script, so do NOT use one of your accounts)

4. On your script put the following a line like the following:

#---
mail \
-S smtp-use-starttls \
-S smtp=smtp://smtp.gmail.com:587 \
-S smtp-auth=login \
-S smtp-auth-user=<username gmail>@gmail.com \
-S smtp-auth-password=<the account password> \
-S from="<username gmail>@gmail.com" \
-S nss-config-dir=<where you stored the certificates DB file> \
-S ssl-verify=ignore \
-s "<email subject>" <to whom the email must be sent>
#---


4.1. If you are in a CentOS box, change the command mail for nail in the above command line and all will work perfectly.

No comments: