-
Notifications
You must be signed in to change notification settings - Fork 0
/
nestedlog-email
executable file
·146 lines (121 loc) · 3 KB
/
nestedlog-email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# Copyright 2021 Stephen Warren <swarren@wwwdotorg.org>
# SPDX-License-Identifier: MIT
set -e
function usage_and_exit {
cat > /dev/stderr <<EOF
usage_and_exit: $0 command...
-f from Email from address.
(Mandatory)
-t to Email to address.
(Mandatory)
(Can be specified multiple times)
-s subject Email subject.
(Mandatory)
command... The command and arguments to execute.
EOF
exit 1
}
mail_from=
mail_tos=()
mail_subject=
while [ $# -gt 0 ]; do
case "$1" in
-f)
if [ -z "$2" ]; then
echo "ERROR: -f requires an argument" > /dev/stderr
usage_and_exit
fi
mail_from="$2"
shift
;;
-t)
if [ -z "$2" ]; then
echo "ERROR: -t requires an argument" > /dev/stderr
usage_and_exit
fi
mail_tos+=("$2")
shift
;;
-s)
if [ -z "$2" ]; then
echo "ERROR: -s requires an argument" > /dev/stderr
usage_and_exit
fi
mail_subject="$2"
shift
;;
-h)
usage_and_exit
;;
--)
shift
break
;;
-*)
echo "Unknown option: $1" > /dev/stderr
usage_and_exit
;;
*)
break
;;
esac
shift
done
if [ -z "${mail_from}" ]; then
echo "ERROR: -f not specified" > /dev/stderr
usage_and_exit
fi
if [ ${#mail_tos} -eq 0 ]; then
echo "ERROR: -t not specified" > /dev/stderr
usage_and_exit
fi
if [ -z "${mail_subject}" ]; then
echo "ERROR: -s not specified" > /dev/stderr
usage_and_exit
fi
function cleanup {
set +e
[ -n "${tmp_dir}" ] && rm -rf "${tmp_dir}"
}
tmp_dir=$(mktemp -d)
trap cleanup exit
log_html="${tmp_dir}/log.html"
log_text="${tmp_dir}/log.txt"
multipart="${tmp_dir}/mulitpart.txt"
set +e
nestedlog log --emit-html-inline "${log_html}" --emit-text "${log_text}" "$@"
cmd_ret=$?
set -e
if [ ${cmd_ret} -eq 0 ]; then
mail_subject="(OK) ${mail_subject}"
elif [ ${cmd_ret} -eq 100 ]; then
mail_subject="(WARNING) ${mail_subject}"
else
mail_subject="(ERROR) ${mail_subject}"
fi
rm -f "${multipart}"
echo -n "Date: " >> "${multipart}"
date -R >> "${multipart}"
for to in "${mail_tos[@]}"; do
echo "To: <${to}>" >> "${multipart}"
done
cat >> "${multipart}" <<EOF
From: <${mail_from}>
Subject: ${mail_subject}
Content-type: multipart/alternative; boundary="XXXXX_multipart_XXXXX"
--XXXXX_multipart_XXXXX
Content-type: text/plain; charset=utf-8
Content-transfer-encoding: 8BIT
EOF
cat >> "${multipart}" < "${log_text}"
cat >> "${multipart}" <<EOF
--XXXXX_multipart_XXXXX
Content-type: text/html; charset=utf-8
Content-transfer-encoding: 8BIT
EOF
cat >> "${multipart}" < "${log_html}"
cat >> "${multipart}" <<EOF
--XXXXX_multipart_XXXXX--
EOF
sendmail -bm "${mail_tos[@]}" < "${multipart}"