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
     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     echo
    50 }
    51 
    52 
    53 #
    54 # print_body subject <message-file> [<attachments>]
    55 #
    56 # prints mime "encoded" message with text attachments to stdout
    57 #
    58 function print_body()
    59 {
    60     local SUBJECT=$1
    61     local BODY=$2
    62     shift 2
    63 
    64     cat <<EOF
    65 Subject: $SUBJECT
    66 Mime-Version: 1.0
    67 Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
    68 
    69 --$MIME_BOUNDARY
    70 Content-Type: text/plain
    71 Content-Transfer-Encoding: 8bit
    72 
    73 EOF
    74     cat $BODY
    75     echo
    76 
    77     for a in $@; do print_attachment $a; done
    78 
    79     echo "--$MIME_BOUNDARY--"
    80     echo 
    81 }
    82 
    83 ## main
    84 
    85 # argument checking
    86 
    87 [ "$1" = "-?" ] && usage
    88 [ "$#" -lt "3" ] && usage
    89 
    90 SUBJECT="$1"
    91 TO="$2"
    92 BODY="$3"
    93 
    94 shift 3
    95 
    96 [ -r "$BODY" ] || fail "could not read $BODY"
    97 
    98 print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"