Admin/isatest/pmail
changeset 22410 da313b67a04d
child 27083 b70c889810cf
equal deleted inserted replaced
22409:5f7c9c82b05e 22410:da313b67a04d
       
     1 #!/usr/bin/env bash
       
     2 #
       
     3 # $Id$
       
     4 # Author: Gerwin Klein, TU Muenchen
       
     5 #
       
     6 # DESCRIPTION: send email with text attachments.
       
     7 # (works for "mail" command of SunOS 5.8)
       
     8 #
       
     9 
       
    10 PRG="$(basename "$0")"
       
    11 
       
    12 MIME_BOUNDARY="==PM_=_37427935"
       
    13 
       
    14 function usage()
       
    15 {
       
    16   echo
       
    17   echo "Usage: $PRG subject recipient <body> [<attachments>]"
       
    18   echo
       
    19   echo "  Send email with text attachments. <body> is a file."
       
    20   echo
       
    21   exit 1
       
    22 }
       
    23 
       
    24 function fail()
       
    25 {
       
    26   echo "$1" >&2
       
    27   exit 2
       
    28 }
       
    29 
       
    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
       
    41 --$MIME_BOUNDARY
       
    42 Content-Type: text/plain
       
    43 Content-Transfer-Encoding: 8bit
       
    44 Content-Disposition: attachment; filename="$NAME"
       
    45 
       
    46 EOF
       
    47     cat $FILE
       
    48     echo
       
    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     echo
       
    75 
       
    76     for a in $@; do print_attachment $a; done
       
    77 
       
    78     echo "--$MIME_BOUNDARY--"
       
    79     echo 
       
    80 }
       
    81 
       
    82 ## main
       
    83 
       
    84 # argument checking
       
    85 
       
    86 [ "$1" = "-?" ] && usage
       
    87 [ "$#" -lt "3" ] && usage
       
    88 
       
    89 SUBJECT="$1"
       
    90 TO="$2"
       
    91 BODY="$3"
       
    92 
       
    93 shift 3
       
    94 
       
    95 [ -r "$BODY" ] || fail "could not read $BODY"
       
    96 
       
    97 print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"