Admin/pmail
author nipkow
Wed, 04 Aug 2004 11:25:08 +0200
changeset 15106 e8cef6993701
parent 14981 e73f8140af78
permissions -rwxr-xr-x
aded comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     1
#!/usr/bin/env bash
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     2
#
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     3
# $Id$
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     4
# Author: Gerwin Klein, TU Muenchen
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     5
#
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     6
# DESCRIPTION: send email with text attachments.
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     7
# (works for "mail" command of SunOS 5.8)
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     8
#
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     9
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    10
PRG="$(basename "$0")"
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    11
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    12
MIME_BOUNDARY="==PM_=_37427935"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    13
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    14
function usage()
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    15
{
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    16
  echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    17
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    18
  echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    19
  echo "  Send email with text attachments. <body> is a file."
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    20
  echo
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    21
  exit 1
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    22
}
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    23
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    24
function fail()
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    25
{
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    26
  echo "$1" >&2
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    27
  exit 2
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    28
}
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    29
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    30
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    31
# print_attachment <file>
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    32
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    33
# print mime "encoded" <file> to stdout (text/plain, 8bit)
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    34
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    35
function print_attachment()
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    36
{
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    37
    local FILE=$1
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    38
    local NAME=${FILE##*/}
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    39
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    40
    cat <<EOF
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    41
--$MIME_BOUNDARY
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    42
Content-Type: text/plain
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    43
Content-Transfer-Encoding: 8bit
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    44
Content-Disposition: attachment; filename="$NAME"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    45
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    46
EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    47
    cat $FILE
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    48
    echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    49
}
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    50
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    51
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    52
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    53
# print_body subject <message-file> [<attachments>]
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    54
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    55
# prints mime "encoded" message with text attachments to stdout
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    56
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    57
function print_body()
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    58
{
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    59
    local SUBJECT=$1
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    60
    local BODY=$2
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    61
    shift 2
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    62
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    63
    cat <<EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    64
Subject: $SUBJECT
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    65
Mime-Version: 1.0
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    66
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    67
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    68
--$MIME_BOUNDARY
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    69
Content-Type: text/plain
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    70
Content-Transfer-Encoding: 8bit
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    71
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    72
EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    73
    cat $BODY
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    74
    echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    75
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    76
    for a in $@; do print_attachment $a; done
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    77
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    78
    echo "--$MIME_BOUNDARY--"
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    79
    echo 
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    80
}
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    81
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    82
## main
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    83
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    84
# argument checking
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    85
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    86
[ "$1" = "-?" ] && usage
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    87
[ "$#" -lt "3" ] && usage
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    88
13321
wenzelm
parents: 13320
diff changeset
    89
SUBJECT="$1"
wenzelm
parents: 13320
diff changeset
    90
TO="$2"
wenzelm
parents: 13320
diff changeset
    91
BODY="$3"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    92
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    93
shift 3
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    94
13321
wenzelm
parents: 13320
diff changeset
    95
[ -r "$BODY" ] || fail "could not read $BODY"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    96
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    97
print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"