2 # |
2 # |
3 # $Id$ |
3 # $Id$ |
4 # Author: Gerwin Klein, TU Muenchen |
4 # Author: Gerwin Klein, TU Muenchen |
5 # License: GPL (GNU GENERAL PUBLIC LICENSE) |
5 # License: GPL (GNU GENERAL PUBLIC LICENSE) |
6 # |
6 # |
7 # DESCRIPTION: send email platform independently. |
7 # DESCRIPTION: send email with text attachments. |
|
8 # (works for "mail" command of SunOS 5.8) |
|
9 # |
8 |
10 |
9 PRG="$(basename "$0")" |
11 PRG="$(basename "$0")" |
|
12 |
|
13 MIME_BOUNDARY="==PM_=_37427935" |
10 |
14 |
11 function usage() |
15 function usage() |
12 { |
16 { |
13 echo |
17 echo |
14 echo "Usage: $PRG subject recipient body" |
18 echo "Usage: $PRG subject recipient <body> [<attachments>]" |
15 echo |
19 echo |
16 echo " Send email platform independently. Body is a file." |
20 echo " Send email with text attachments. <body> is a file." |
17 echo |
21 echo |
18 exit 1 |
22 exit 1 |
19 } |
23 } |
20 |
24 |
21 function fail() |
25 function fail() |
22 { |
26 { |
23 echo "$1" >&2 |
27 echo "$1" >&2 |
24 exit 2 |
28 exit 2 |
25 } |
29 } |
26 |
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 } |
|
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 |
|
75 for a in $@; do print_attachment $a; done |
|
76 |
|
77 echo "$MIME_BOUNDARY" |
|
78 } |
|
79 |
27 ## main |
80 ## main |
28 |
81 |
29 # argument checking |
82 # argument checking |
30 |
83 |
31 [ "$1" = "-?" ] && usage |
84 [ "$1" = "-?" ] && usage |
32 [ "$#" != "3" ] && usage |
85 [ "$#" -lt "3" ] && usage |
33 |
86 |
34 SUBJECT="$1" |
87 SUBJECT="$1" |
35 TO="$2" |
88 TO="$2" |
36 BODY="$3" |
89 BODY="$3" |
37 |
90 |
|
91 shift 3 |
|
92 |
38 [ -r "$BODY" ] || fail "could not read $BODY" |
93 [ -r "$BODY" ] || fail "could not read $BODY" |
39 |
94 |
40 case `uname` in |
95 print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO" |
41 linux*|Linux*) |
|
42 mail -s "$SUBJECT" "$TO" < "$BODY" |
|
43 ;; |
|
44 |
|
45 SunOS*) |
|
46 mail -t "$TO" <<EOF |
|
47 Subject: $SUBJECT |
|
48 |
|
49 `cat $BODY` |
|
50 EOF |
|
51 ;; |
|
52 |
|
53 *) |
|
54 fail "unkown platform" |
|
55 ;; |
|
56 esac |
|