Admin/pmail
author paulson
Tue, 10 Feb 2004 12:02:11 +0100
changeset 14378 69c4d5997669
parent 14036 fb6040d4bbf8
child 14981 e73f8140af78
permissions -rwxr-xr-x
generic of_nat and of_int functions, and generalization of iszero and neg
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
# License: GPL (GNU GENERAL PUBLIC LICENSE)
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
     6
#
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     7
# DESCRIPTION: send email with text attachments.
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     8
# (works for "mail" command of SunOS 5.8)
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
     9
#
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    10
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    11
PRG="$(basename "$0")"
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    12
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    13
MIME_BOUNDARY="==PM_=_37427935"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    14
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    15
function usage()
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    16
{
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    17
  echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    18
  echo "Usage: $PRG subject recipient <body> [<attachments>]"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    19
  echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    20
  echo "  Send email with text attachments. <body> is a file."
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    21
  echo
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    22
  exit 1
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    23
}
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    24
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    25
function fail()
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    26
{
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    27
  echo "$1" >&2
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    28
  exit 2
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    29
}
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    30
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    31
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    32
# print_attachment <file>
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    33
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    34
# print mime "encoded" <file> to stdout (text/plain, 8bit)
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    35
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    36
function print_attachment()
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    37
{
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    38
    local FILE=$1
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    39
    local NAME=${FILE##*/}
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    40
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    41
    cat <<EOF
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    42
--$MIME_BOUNDARY
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    43
Content-Type: text/plain
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    44
Content-Transfer-Encoding: 8bit
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    45
Content-Disposition: attachment; filename="$NAME"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    46
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    47
EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    48
    cat $FILE
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    49
    echo
14034
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
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    54
# print_body subject <message-file> [<attachments>]
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    55
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    56
# prints mime "encoded" message with text attachments to stdout
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    57
#
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    58
function print_body()
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    59
{
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    60
    local SUBJECT=$1
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    61
    local BODY=$2
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    62
    shift 2
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    63
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    64
    cat <<EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    65
Subject: $SUBJECT
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    66
Mime-Version: 1.0
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    67
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY"
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    68
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    69
--$MIME_BOUNDARY
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    70
Content-Type: text/plain
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    71
Content-Transfer-Encoding: 8bit
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    72
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    73
EOF
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    74
    cat $BODY
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    75
    echo
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    76
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    77
    for a in $@; do print_attachment $a; done
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    78
14036
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    79
    echo "--$MIME_BOUNDARY--"
fb6040d4bbf8 fix mime stuff
kleing
parents: 14034
diff changeset
    80
    echo 
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    81
}
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    82
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    83
## main
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    84
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    85
# argument checking
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    86
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    87
[ "$1" = "-?" ] && usage
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    88
[ "$#" -lt "3" ] && usage
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    89
13321
wenzelm
parents: 13320
diff changeset
    90
SUBJECT="$1"
wenzelm
parents: 13320
diff changeset
    91
TO="$2"
wenzelm
parents: 13320
diff changeset
    92
BODY="$3"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    93
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    94
shift 3
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    95
13321
wenzelm
parents: 13320
diff changeset
    96
[ -r "$BODY" ] || fail "could not read $BODY"
13320
2c6ee189ae63 send email plaform independently
isatest
parents:
diff changeset
    97
14034
55ba81e3502b support text file attachments
kleing
parents: 13321
diff changeset
    98
print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"