Thursday, August 13, 2009

Sending Emails from the commandline Part 2

Previously I had posted about my challenges in getting attachments sent and being seen reliably as one in most MUAs. Spending a few more days pottering around and testing, I finally settled on mpack. Using mutt, I could not get it to work consistently in an cygwin environment as it kept dumping core.

I downloaded mpack, ran make and then installed it. Based on the manpage and a few examples on the web, I rejigged my shell script use mpack to generate a MIME encoded mail message, wrapped with msmtp needed headers and mailed it out. Works flawlessly.

So far.

The code snippet below is the way I used it.
function send_it () {
/usr/sbin/msmtp -t < $1
echo "mail send status" $?
if [ $? -ne 0 ]; then
echo "Fail: $1 file not deleted"
else
echo "Success: Removing $1 file"
rm $1
fi
}

function mpack_it () {
# outmimefile, zip file name, tmpfile split_part
mpack -s "Generated Output $4 : `date` " -o $1 $2
#change inline to attachment, insert msmtp needed headers and delete message-ID
sed 's/inline/attachment/' $1 | sed "/^Subject:/i\
To: \nFrom: xxxxxx@gmail.com \nBcc: $LIST"|sed '1,1d' > $3

}


The attachment itself is base64 encoded.

This solution, I'm reasonably satisfied with, though I'm pretty sure when I try an all Win32 solution, it might flame out spectacularly.