Ads by Google

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 | /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  -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?


Unknown said...

mimeit, in the metamail package, takes stdin and mails it mime-attached. You can even configure splitting.

sivaram said...

Thanks, I'll give it a shot and see how it turns