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