Misc: Sending attachments from command lineΒΆ

Title:Misc: Sending attachments from command line
Author:Douglas O’Leary <dkoleary@olearycomputers.com>
Description:Misc: Sending attachments from command line
Date created:02/2001
Date updated:08/2010
Disclaimer:Standard: Use the information that follows at your own risk. If you screw up a system, don’t blame it on me...

The outline below shows how to send html and/or text files as attachments to mail and it works. I had problems, recently, getting it to work with gzip’ed tarballs. The attachment would come through, but it would be corrupted - winzip hated it. I have a suspicion it’s the antiquated version of uuencode that comes with HPUX.

After quite a bit of searching, I found mpack on one of the hp porting centers. This guy’s much simpler than the process below. The command line looks like:

mpack -s ${subject} ${file} ${recipient}

It’s got some limitations; doesn’t appear to support any method of changing the sendor name (similar to -r arg to mailx command). But, it does what it does very well; have to say I’m impressed

One of the limitations is that I’m running this crap on hpux. That tends to have a limited subset of the normal gnu or freeware utilities. Other OSes would probably do well with mutt or some of the other solutions I saw via web searches

If you want to go hard core, the mime format for sending normal html or text based messages as attachments is as follows:

cat << eof
From: root@omniserv.joe-blow.com
To: doleary@joe-blow.com
Subject: Omnireport: ${Date}
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="i_hart_dkoleary"

This is a test of the emergency broadcasting system.
This is only a test.

Had this been an actual emergency, you'd be instructed to
kiss your ass goodbye.

This was a test.

Content-Type: text/plain; name="omnirpt_${Date_ext}.html"; charset=US-ASCII
Content-Disposition: attachment; filename="omnirpt_${Date_ext}.html"
Content-Transfer-Encoding: 7bit

cat ${Tmp}

echo "--${Bound}"

) | sendmail -t -v

Obviously, there are LOTS of alternatives to the scripting; however, its’ the mime format that’s important. Things of note:

  1. To: Multiple addresses should be separated by commas.
  2. Bounday entires are defined in the first content-type line.
  3. Bounday entries in the text start with the initial – (dash dash).
  4. Content-Disposition: attachment; makes the text the attachment.
  5. filename=”omnirpt_${Date_ext}.html” gives the attachment the filename on the remote side.
  6. sendmail -t causes sendmail to read the recipients from the message itself instead of from the command line.