Admin/isatest/pmail
author paulson
Tue, 02 Mar 2010 12:59:16 +0000
changeset 35509 13e83ce8391b
parent 31582 4753c317d5c1
child 47104 b48d8f7f50fb
permissions -rwxr-xr-x
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22410
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     1
#!/usr/bin/env bash
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     2
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     3
# Author: Gerwin Klein, TU Muenchen
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     4
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     5
# DESCRIPTION: send email with text attachments.
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     6
# (works for "mail" command of SunOS 5.8)
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     7
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     8
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     9
PRG="$(basename "$0")"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    10
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    11
MIME_BOUNDARY="==PM_=_37427935"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    12
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    13
function usage()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    14
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    15
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    16
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    17
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    18
  echo "  Send email with text attachments. <body> is a file."
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    19
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    20
  exit 1
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    21
}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    22
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    23
function fail()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    24
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    25
  echo "$1" >&2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    26
  exit 2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    27
}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    28
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    29
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    30
# print_attachment <file>
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    31
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    32
# print mime "encoded" <file> to stdout (text/plain, 8bit)
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    33
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    34
function print_attachment()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    35
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    36
    local FILE=$1
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    37
    local NAME=${FILE##*/}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    38
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    39
    cat <<EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    40
--$MIME_BOUNDARY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    41
Content-Type: text/plain
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    42
Content-Transfer-Encoding: 8bit
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    43
Content-Disposition: attachment; filename="$NAME"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    44
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    45
EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    46
    cat $FILE
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    47
    echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    48
}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    49
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    50
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    51
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    52
# print_body subject <message-file> [<attachments>]
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    53
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    54
# prints mime "encoded" message with text attachments to stdout
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    55
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    56
function print_body()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    57
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    58
    local SUBJECT=$1
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    59
    local BODY=$2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    60
    shift 2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    61
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    62
    cat <<EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    63
Subject: $SUBJECT
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    64
Mime-Version: 1.0
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    65
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    66
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    67
--$MIME_BOUNDARY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    68
Content-Type: text/plain
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    69
Content-Transfer-Encoding: 8bit
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    70
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    71
EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    72
    cat $BODY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    73
    echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    74
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    75
    for a in $@; do print_attachment $a; done
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    76
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    77
    echo "--$MIME_BOUNDARY--"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    78
    echo 
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    79
}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    80
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    81
## main
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    82
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    83
# argument checking
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    84
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    85
[ "$1" = "-?" ] && usage
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    86
[ "$#" -lt "3" ] && usage
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    87
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    88
SUBJECT="$1"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    89
TO="$2"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    90
BODY="$3"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    91
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    92
shift 3
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    93
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    94
[ -r "$BODY" ] || fail "could not read $BODY"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    95
27083
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
    96
case `uname` in
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
    97
	Linux)  for F in $@; do ATTACH="$ATTACH -a $F"; done
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
    98
		cat "$BODY" | mail -s "$SUBJECT" $ATTACH "$TO"
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
    99
		;;
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
   100
	SunOS)
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
   101
		print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
   102
		;;
b70c889810cf send from linux systems as well
isatest
parents: 22410
diff changeset
   103
esac