author | blanchet |
Fri, 20 Sep 2013 16:32:27 +0200 | |
changeset 53753 | ae7f50e70c09 |
parent 48252 | e98c3d50ae62 |
permissions | -rwxr-xr-x |
22410
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
1 |
#!/usr/bin/env bash |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
2 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
3 |
# Author: Gerwin Klein, TU Muenchen |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
4 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
5 |
# DESCRIPTION: send email with text attachments. |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
6 |
# (works for "mail" command of SunOS 5.8) |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
7 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
8 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
9 |
PRG="$(basename "$0")" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
10 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
11 |
MIME_BOUNDARY="==PM_=_37427935" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
12 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
13 |
function usage() |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
14 |
{ |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
15 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
16 |
echo "Usage: $PRG subject recipient <body> [<attachments>]" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
17 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
18 |
echo " Send email with text attachments. <body> is a file." |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
19 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
20 |
exit 1 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
21 |
} |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
22 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
23 |
function fail() |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
24 |
{ |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
25 |
echo "$1" >&2 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
26 |
exit 2 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
27 |
} |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
28 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
29 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
30 |
# print_attachment <file> |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
31 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
32 |
# print mime "encoded" <file> to stdout (text/plain, 8bit) |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
33 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
34 |
function print_attachment() |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
35 |
{ |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
36 |
local FILE=$1 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
37 |
local NAME=${FILE##*/} |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
38 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
39 |
cat <<EOF |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
40 |
--$MIME_BOUNDARY |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
41 |
Content-Type: text/plain |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
42 |
Content-Transfer-Encoding: 8bit |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
43 |
Content-Disposition: attachment; filename="$NAME" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
44 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
45 |
EOF |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
46 |
cat $FILE |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
47 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
48 |
} |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
49 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
50 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
51 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
52 |
# print_body subject <message-file> [<attachments>] |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
53 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
54 |
# prints mime "encoded" message with text attachments to stdout |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
55 |
# |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
56 |
function print_body() |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
57 |
{ |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
58 |
local SUBJECT=$1 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
59 |
local BODY=$2 |
48252
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
60 |
local TO=$3 |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
61 |
shift 3 |
22410
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
62 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
63 |
cat <<EOF |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
64 |
Subject: $SUBJECT |
48252
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
65 |
To: $TO |
22410
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
66 |
Mime-Version: 1.0 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
67 |
Content-Type: multipart/mixed; boundary="$MIME_BOUNDARY" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
68 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
69 |
--$MIME_BOUNDARY |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
70 |
Content-Type: text/plain |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
71 |
Content-Transfer-Encoding: 8bit |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
72 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
73 |
EOF |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
74 |
cat $BODY |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
75 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
76 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
77 |
for a in $@; do print_attachment $a; done |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
78 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
79 |
echo "--$MIME_BOUNDARY--" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
80 |
echo |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
81 |
} |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
82 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
83 |
## main |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
84 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
85 |
# argument checking |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
86 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
87 |
[ "$1" = "-?" ] && usage |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
88 |
[ "$#" -lt "3" ] && usage |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
89 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
90 |
SUBJECT="$1" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
91 |
TO="$2" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
92 |
BODY="$3" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
93 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
94 |
shift 3 |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
95 |
|
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
96 |
[ -r "$BODY" ] || fail "could not read $BODY" |
da313b67a04d
moved all isatest/cron job related files to own directory
kleing
parents:
diff
changeset
|
97 |
|
48252
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
98 |
case `hostname` in |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
99 |
lxbroy*) |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
100 |
print_body "$SUBJECT" "$BODY" "$TO" $@ | sendmail "$TO" |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
101 |
;; |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
102 |
macbroy*) for F in $@; do ATTACH="$ATTACH -a $F"; done |
48172
41222a782780
not all Linuxen understand -Ssmpt= and defaults seem to be configured correctly now
kleing
parents:
47104
diff
changeset
|
103 |
cat "$BODY" | mail -s "$SUBJECT" $ATTACH "$TO" |
27083 | 104 |
;; |
48252
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
105 |
sunbroy*) |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
106 |
print_body "$SUBJECT" "$BODY" "$TO" $@ | mail -t "$TO" |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
107 |
;; |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
108 |
*) |
e98c3d50ae62
get attachments sent even on lxbroy Gentoo machines
Gerwin Klein <gerwin.klein@nicta.com.au>
parents:
48172
diff
changeset
|
109 |
fail "unknown host/platform" |
27083 | 110 |
;; |
111 |
esac |