Sunday, January 15, 2012

Sending Emails with Attachments from the Command Line Revisited

Previously I had posted about trying to send attachment emails from the command line here and here.  But the hitch was, every time I changed computers(which while infrequent) I still had to download and compile mpack and install it every time.

On a whim, when I asked on the cygwin mailing list, I was asked to try the Email package.  And I have to report it works beautifully.  It needs an MTA to connect to the mail server, for which there is msmtp and is part of the Cygwin packaged utilities already.   That sort of solves my portability issue.

Configuring Email was simple.  Edit the email.conf file with the following minimum details and you're set.


############################################################
SENDMAIL_BIN = '/usr/sbin/msmtp -t'
MY_NAME  = 'Sivaram '
MY_EMAIL = 'nnsivaram.nnet@gmail.com'
#REPLY_TO = ''
USE_TLS = 'true'
TEMP_DIR = '/tmp'
SMTP_AUTH = 'LOGIN'
SMTP_AUTH_USER = ' nnsivaram.nnet

And you call it in the shell script as follows


    echo "Mailing... $mailid with $attachfile"


    /usr/bin/email  -s "Mailed report on `date`" -a $attachfile  $mailid < $mbody
    echo "email Return code is $?"