Admin/isatest/pmail
author urbanc
Thu, 13 Sep 2007 23:58:38 +0200
changeset 24571 a6d0428dea8e
parent 22410 da313b67a04d
child 27083 b70c889810cf
permissions -rwxr-xr-x
some cleaning up to do with contexts
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
# $Id$
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     4
# Author: Gerwin Klein, TU Muenchen
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     5
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     6
# DESCRIPTION: send email with text attachments.
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
     7
# (works for "mail" command of SunOS 5.8)
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
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    10
PRG="$(basename "$0")"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    11
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    12
MIME_BOUNDARY="==PM_=_37427935"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    13
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    14
function usage()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    15
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    16
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    17
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    18
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    19
  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
    20
  echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    21
  exit 1
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
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    24
function fail()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    25
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    26
  echo "$1" >&2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    27
  exit 2
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
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    31
# print_attachment <file>
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    32
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    33
# print mime "encoded" <file> to stdout (text/plain, 8bit)
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    34
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    35
function print_attachment()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    36
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    37
    local FILE=$1
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    38
    local NAME=${FILE##*/}
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    39
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    40
    cat <<EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    41
--$MIME_BOUNDARY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    42
Content-Type: text/plain
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    43
Content-Transfer-Encoding: 8bit
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    44
Content-Disposition: attachment; filename="$NAME"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    45
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    46
EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    47
    cat $FILE
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    48
    echo
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
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    53
# print_body subject <message-file> [<attachments>]
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    54
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    55
# prints mime "encoded" message with text attachments to stdout
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    56
#
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    57
function print_body()
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    58
{
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    59
    local SUBJECT=$1
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    60
    local BODY=$2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    61
    shift 2
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    62
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    63
    cat <<EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    64
Subject: $SUBJECT
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    65
Mime-Version: 1.0
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    66
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    67
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    68
--$MIME_BOUNDARY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    69
Content-Type: text/plain
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    70
Content-Transfer-Encoding: 8bit
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    71
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    72
EOF
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    73
    cat $BODY
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    74
    echo
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    75
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    76
    for a in $@; do print_attachment $a; done
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    77
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    78
    echo "--$MIME_BOUNDARY--"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    79
    echo 
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
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    82
## main
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    83
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    84
# argument checking
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    85
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    86
[ "$1" = "-?" ] && usage
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    87
[ "$#" -lt "3" ] && usage
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    88
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    89
SUBJECT="$1"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    90
TO="$2"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    91
BODY="$3"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    92
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    93
shift 3
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    94
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    95
[ -r "$BODY" ] || fail "could not read $BODY"
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    96
da313b67a04d moved all isatest/cron job related files to own directory
kleing
parents:
diff changeset
    97
print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"