Admin/pmail
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 14981 e73f8140af78
permissions -rwxr-xr-x
setup: theory -> theory;
isatest@13320
     1
#!/usr/bin/env bash
isatest@13320
     2
#
isatest@13320
     3
# $Id$
isatest@13320
     4
# Author: Gerwin Klein, TU Muenchen
isatest@13320
     5
#
kleing@14034
     6
# DESCRIPTION: send email with text attachments.
kleing@14034
     7
# (works for "mail" command of SunOS 5.8)
kleing@14034
     8
#
isatest@13320
     9
isatest@13320
    10
PRG="$(basename "$0")"
isatest@13320
    11
kleing@14034
    12
MIME_BOUNDARY="==PM_=_37427935"
kleing@14034
    13
isatest@13320
    14
function usage()
isatest@13320
    15
{
isatest@13320
    16
  echo
kleing@14034
    17
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
isatest@13320
    18
  echo
kleing@14034
    19
  echo "  Send email with text attachments. <body> is a file."
isatest@13320
    20
  echo
isatest@13320
    21
  exit 1
isatest@13320
    22
}
isatest@13320
    23
isatest@13320
    24
function fail()
isatest@13320
    25
{
isatest@13320
    26
  echo "$1" >&2
isatest@13320
    27
  exit 2
isatest@13320
    28
}
isatest@13320
    29
kleing@14034
    30
#
kleing@14034
    31
# print_attachment <file>
kleing@14034
    32
#
kleing@14034
    33
# print mime "encoded" <file> to stdout (text/plain, 8bit)
kleing@14034
    34
#
kleing@14034
    35
function print_attachment()
kleing@14034
    36
{
kleing@14034
    37
    local FILE=$1
kleing@14034
    38
    local NAME=${FILE##*/}
kleing@14034
    39
kleing@14034
    40
    cat <<EOF
kleing@14036
    41
--$MIME_BOUNDARY
kleing@14034
    42
Content-Type: text/plain
kleing@14034
    43
Content-Transfer-Encoding: 8bit
kleing@14034
    44
Content-Disposition: attachment; filename="$NAME"
kleing@14034
    45
kleing@14034
    46
EOF
kleing@14034
    47
    cat $FILE
kleing@14036
    48
    echo
kleing@14034
    49
}
kleing@14034
    50
kleing@14034
    51
kleing@14034
    52
#
kleing@14034
    53
# print_body subject <message-file> [<attachments>]
kleing@14034
    54
#
kleing@14034
    55
# prints mime "encoded" message with text attachments to stdout
kleing@14034
    56
#
kleing@14034
    57
function print_body()
kleing@14034
    58
{
kleing@14034
    59
    local SUBJECT=$1
kleing@14034
    60
    local BODY=$2
kleing@14034
    61
    shift 2
kleing@14034
    62
kleing@14034
    63
    cat <<EOF
kleing@14034
    64
Subject: $SUBJECT
kleing@14034
    65
Mime-Version: 1.0
kleing@14034
    66
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
kleing@14034
    67
kleing@14036
    68
--$MIME_BOUNDARY
kleing@14034
    69
Content-Type: text/plain
kleing@14034
    70
Content-Transfer-Encoding: 8bit
kleing@14034
    71
kleing@14034
    72
EOF
kleing@14034
    73
    cat $BODY
kleing@14036
    74
    echo
kleing@14034
    75
kleing@14034
    76
    for a in $@; do print_attachment $a; done
kleing@14034
    77
kleing@14036
    78
    echo "--$MIME_BOUNDARY--"
kleing@14036
    79
    echo 
kleing@14034
    80
}
kleing@14034
    81
isatest@13320
    82
## main
isatest@13320
    83
isatest@13320
    84
# argument checking
isatest@13320
    85
isatest@13320
    86
[ "$1" = "-?" ] && usage
kleing@14034
    87
[ "$#" -lt "3" ] && usage
isatest@13320
    88
wenzelm@13321
    89
SUBJECT="$1"
wenzelm@13321
    90
TO="$2"
wenzelm@13321
    91
BODY="$3"
isatest@13320
    92
kleing@14034
    93
shift 3
kleing@14034
    94
wenzelm@13321
    95
[ -r "$BODY" ] || fail "could not read $BODY"
isatest@13320
    96
kleing@14034
    97
print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"