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