sabato 27 dicembre 2008

Mutt and nbSMTP

Not so long ago I installed in different locations a few PCs as servers.
I choose Ubuntu server edition 8.0.4 LTS (Long-Term Support) aka Hardy Heron, the latest LTS version available from Canonical.
I prefer the hard way, without the GUI. Sorry, I don't like Linux GUIs ;-)
That way I can control what is installed, and install just what is really useful for that particular machine, leaving precious resources to services that need them. There is also another advantage. Limiting possible security issues.
One of these servers, in particular, has to manage the nightly backup of another server, this time a Windows 2003, via rsync.
I created a bash script to do the job and scheduled it with crontab.
The script, during the process, creates a report that is saved as a zip archive when the work is finished.
I was wondering how could I get that file to check everything was fine, without remotely access the server.
The obvious response was. By e-mail.
I started hunting for information in search for the most elegant solution with the minimum impact on the system.

What I found, and I picked, to achieve my goal, are these two little gems.

Mutt "a small but very powerful text-based mail client for Unix operating systems" and No Brainer SMTP nbSMTP "a simple MTA to send your mails to another mail server via SMTP".

First step. Retrieve and install the two packages. In Ubunto the apt-get command is aimed at that.

sudo apt-get install mutt
sudo apt-get install nbsmtp

I created a user specific .muttrc containing these lines

# No need to keep copy of the outgoing messages
set copy=no
# Some changes to the message headers
set hdrs=yes
my_hdr From: Me the sender <me@mydomain.com>
# How to invoke nbsmtp
set sendmail="/usr/bin/nbsmtp -f me@mydomain.com -d mydomain.com -h my_isp_smtp_server.com"

and to complete the work, at the end of the bash script that is supposed to make the backup, I've added the line in charge of sending the message

mutt -s "Backup report" -a report.zip me@mydomain.com < messagebody.txt

"Backup report" - is the subject of the message (option -s)
report.zip - is the attachment (option -a)
me@mydomain.com - is the recipient
messagebody.txt - is the main body of the e-mail (read message from the file messagebody.txt)

The file messagebody.txt could be anything you want to insert as the body of the message being sent.

Last but not least :-) I've also inserted this line into crontab file

MAILTO=""

to avoid error warning in log file
nbSMTP does not support the -o arguments from sendmail, ignoring them

because every time a croned job is executed mail is sent to the owner of the crontab