Ads by Google

Showing posts with label mutt. Show all posts
Showing posts with label mutt. Show all posts

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.

Saturday, August 8, 2009

Sending attachments from the command line

I spent about 2 hours trying to get this sorted out but I'm pretty much unsatisfied with  the results.

I want to send emails with largish attachments from a script.  And I use msmtp as my MTA.  After reading up things and trail and error, this works for me.

$( cat ./mail.txt; uuencode output.zip output.zip) | /usr/sbin/msmtp -d -t

where  mail.txt has the From,To,Subject and body text and the .msmtprc file has the mailserver userid and password details to connect to the smtp server.  Since I use the cygwin based msmtp, as a script this sends out emails as desired.

The problem is that, the file is not recognised as an attachment in some mail clients and sometimes the encoded file is inline in the body of the text.  On Lotus Notes of my colleague, it appears an attachment in the mail body but he sees no attachment icon to identify it in the folder.  In Gmail  Sent Mail folder, the attachment is completely inline in the mail body and that is significantly going to slow opening those mails in the browser.

Reading up a bit more, I sort of figured, that the attachment needs to be MIME encoded for the attachment to reliably seen as attachments in most MUAs.

Asking around with the author of msmtp, it turns out MIME support is more of MUA thing rather than the MTA.  He suggested using mutt or any other MUA to accomplish the same.  So I have

mutt -s "$SUBJ" -a A_N_scrips.zip  -b "$LIST" < $TMPFILE
where LIST is the list of email addresses and TMPFILE is the message body.  This works except that on large attachments, mutt seems to dump core.  I worked around that by splitting the attachments and sending it in 2 different emails.  Not good but enough to send the files across.  Of course, you need an .muttrc file to be configured to use msmtp to send the mail.

But I think, using an MUA to send a MIME encoded file is a bit much.

Are there any other methods to do it?  Without using Perl?