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