Admin/pmail
author kleing
Sun May 18 15:15:13 2003 +0200 (2003-05-18)
changeset 14034 55ba81e3502b
parent 13321 70a5d5fc081a
child 14036 fb6040d4bbf8
permissions -rwxr-xr-x
support text file attachments
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@14034
    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@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@14034
    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@14034
    74
kleing@14034
    75
    for a in $@; do print_attachment $a; done
kleing@14034
    76
kleing@14034
    77
    echo "$MIME_BOUNDARY"
kleing@14034
    78
}
kleing@14034
    79
isatest@13320
    80
## main
isatest@13320
    81
isatest@13320
    82
# argument checking
isatest@13320
    83
isatest@13320
    84
[ "$1" = "-?" ] && usage
kleing@14034
    85
[ "$#" -lt "3" ] && usage
isatest@13320
    86
wenzelm@13321
    87
SUBJECT="$1"
wenzelm@13321
    88
TO="$2"
wenzelm@13321
    89
BODY="$3"
isatest@13320
    90
kleing@14034
    91
shift 3
kleing@14034
    92
wenzelm@13321
    93
[ -r "$BODY" ] || fail "could not read $BODY"
isatest@13320
    94
kleing@14034
    95
print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"