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