#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   Send file attachments as MIME mail
# author
#   OG 15/04/04
# description
#
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
#
usage()
{
  exec >&2
  echo "Usage: $0 [-u] [-s subject] [-t mime-type] [-f from] [-n name]"
  echo "       recipient [file]"
  echo
  echo "Examples:"
  echo "  $0 -s \"Web page\" -t text/html webmaster test.html"
  echo "  $0 -s \"Image\" -t image/png -u user01@isp01.com test.png"
  echo "  ps -ef | $0 -s \"Process list\" -n processes.txt root"
  echo
  exit 1
}

test $# -lt 1 && usage

# Defaults

uuencode=false
type=text/plain
name=attachment
from=user02@isp01.com
subject="Attached file"
boundary=unique-boundary-1

# Process command-line arguments

while test $# -gt 0; do
  case $1 in
    -u) uuencode=true; shift;;
    -s) subject=$2; shift 2;;
    -t) type=$2; shift 2;;
    -f) from=$2; shift 2;;
    -n) name=$2; shift 2;;
    *) victim=$1; file=$2; shift $#;;
  esac
done

# Check to see if we picked up the victim

test "$victim" = "" && usage

# If we have a filename, then use that as the attachment name

test "$name" = "attachment" && test "$file" != "" && name=$file

#
# Format and send mail

(
cat << EOT
To: $victim
From: $from
Subject: $subject
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=$boundary

--$boundary

Please see attached file $file
--$boundary
Content-Type: $type
Content-Disposition: attachment; filename=$name
EOT

# Send actual file (possibly uuencoded)

if [ "$uuencode" = "false" ]; then
  echo
  cat $file
else
  echo "Content-Transfer-Encoding: uuencode"
  echo
  cat $file | uuencode $file
fi

echo --$boundary--

) | /usr/lib/sendmail $victim
