Admin/isatest/pmail
author blanchet
Mon, 19 May 2014 23:43:53 +0200
changeset 57005 33f3d2ea803d
parent 48252 e98c3d50ae62
permissions -rwxr-xr-x
store all MaSh data on the Isabelle side, in preparation for replacing 'mash.py' with ML solution

#!/usr/bin/env bash
#
# Author: Gerwin Klein, TU Muenchen
#
# DESCRIPTION: send email with text attachments.
# (works for "mail" command of SunOS 5.8)
#

PRG="$(basename "$0")"

MIME_BOUNDARY="==PM_=_37427935"

function usage()
{
  echo
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
  echo
  echo "  Send email with text attachments. <body> is a file."
  echo
  exit 1
}

function fail()
{
  echo "$1" >&2
  exit 2
}

#
# print_attachment <file>
#
# print mime "encoded" <file> to stdout (text/plain, 8bit)
#
function print_attachment()
{
    local FILE=$1
    local NAME=${FILE##*/}

    cat <<EOF
--$MIME_BOUNDARY
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="$NAME"

EOF
    cat $FILE
    echo
}


#
# print_body subject <message-file> [<attachments>]
#
# prints mime "encoded" message with text attachments to stdout
#
function print_body()
{
    local SUBJECT=$1
    local BODY=$2
    local TO=$3
    shift 3

    cat <<EOF
Subject: $SUBJECT
To: $TO
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"

--$MIME_BOUNDARY
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

EOF
    cat $BODY
    echo

    for a in $@; do print_attachment $a; done

    echo "--$MIME_BOUNDARY--"
    echo 
}

## main

# argument checking

[ "$1" = "-?" ] && usage
[ "$#" -lt "3" ] && usage

SUBJECT="$1"
TO="$2"
BODY="$3"

shift 3

[ -r "$BODY" ] || fail "could not read $BODY"

case `hostname` in
	lxbroy*)
		print_body "$SUBJECT" "$BODY" "$TO" $@ | sendmail "$TO"
		;;
	macbroy*)  for F in $@; do ATTACH="$ATTACH -a $F"; done
		cat "$BODY" | mail -s "$SUBJECT" $ATTACH "$TO"
		;;
	sunbroy*)
		print_body "$SUBJECT" "$BODY" "$TO" $@ | mail -t "$TO"
		;;
	*)
		fail "unknown host/platform"
		;;
esac