1 |
#!/bin/sh |
---|
2 |
############################################################################### |
---|
3 |
## |
---|
4 |
## This is a more general script library for sending |
---|
5 |
## E-Mail on the Thecus N5200. |
---|
6 |
## |
---|
7 |
## The main function is MailSend(). |
---|
8 |
## |
---|
9 |
## 2007-01-16 Andreas Vogel (omega) |
---|
10 |
## |
---|
11 |
## Changes: |
---|
12 |
## 09.01.2007 0.1 initial version |
---|
13 |
## 31.05.2007 0.2 change mail from for FW >= 1.00.10 (by Peter Futterknecht (peterfu)) |
---|
14 |
## |
---|
15 |
############################################################################### |
---|
16 |
|
---|
17 |
msmtp=/opt/bin/msmtp |
---|
18 |
sqlite=/opt/bin/sqlite |
---|
19 |
confdb=/app/cfg/conf.db |
---|
20 |
|
---|
21 |
MailGetRecipients() { |
---|
22 |
if [ -z "$mailRecipients" ] ; then |
---|
23 |
sqlcmd="select v from conf where k like 'notif_addr%'" |
---|
24 |
mailRecipients=$($sqlite $confdb "$sqlcmd" | tr "\n" " ") |
---|
25 |
fi |
---|
26 |
echo $mailRecipients |
---|
27 |
} |
---|
28 |
|
---|
29 |
MailGetAuthName() { |
---|
30 |
if [ -z "$mailAuthName" ] ; then |
---|
31 |
sqlcmd="select v from conf where k='notif_account'" |
---|
32 |
mailAuthName=$($sqlite $confdb "$sqlcmd") |
---|
33 |
fi |
---|
34 |
echo $mailAuthName |
---|
35 |
} |
---|
36 |
|
---|
37 |
MailGetAuthPasswd() { |
---|
38 |
if [ -z "$mailAuthPasswd" ] ; then |
---|
39 |
sqlcmd="select v from conf where k='notif_password'" |
---|
40 |
mailAuthPasswd=$($sqlite $confdb "$sqlcmd") |
---|
41 |
fi |
---|
42 |
echo $mailAuthPasswd |
---|
43 |
} |
---|
44 |
|
---|
45 |
MailGetAuthMethod() { |
---|
46 |
if [ -z "$mailAuthMethod" ] ; then |
---|
47 |
sqlcmd="select v from conf where k='notif_auth'" |
---|
48 |
mailAuthMethod=$($sqlite $confdb "$sqlcmd") |
---|
49 |
fi |
---|
50 |
echo $mailAuthMethod |
---|
51 |
} |
---|
52 |
|
---|
53 |
MailGetServer() { |
---|
54 |
if [ -z "$mailServer" ] ; then |
---|
55 |
sqlcmd="select v from conf where k='notif_smtp'" |
---|
56 |
mailServer=$($sqlite $confdb "$sqlcmd") |
---|
57 |
fi |
---|
58 |
echo $mailServer |
---|
59 |
} |
---|
60 |
|
---|
61 |
MailGetPort() { |
---|
62 |
if [ -z "$mailPort" ] ; then |
---|
63 |
sqlcmd="select v from conf where k='notif_smtport'" |
---|
64 |
mailPort=$($sqlite $confdb "$sqlcmd") |
---|
65 |
fi |
---|
66 |
echo $mailPort |
---|
67 |
} |
---|
68 |
|
---|
69 |
MailGetNotify() { |
---|
70 |
if [ -z "$mailNotify" ] ; then |
---|
71 |
if [ -n "$1" ];then |
---|
72 |
field="notif_$1" |
---|
73 |
sqlcmd="select v from conf where k='$field'" |
---|
74 |
mailNotify=$($sqlite $confdb "$sqlcmd") |
---|
75 |
fi |
---|
76 |
fi |
---|
77 |
echo $mailNotify |
---|
78 |
} |
---|
79 |
|
---|
80 |
MailGetFrom () { |
---|
81 |
if [ -z "$mailFrom" ] ; then |
---|
82 |
sqlcmd="select v from conf where k='notif_from'" |
---|
83 |
mailFrom=$($sqlite $confdb "$sqlcmd") |
---|
84 |
fi |
---|
85 |
if [ -z "$mailFrom" ] ; then |
---|
86 |
mailFrom="thecus@$(hostname)" |
---|
87 |
fi |
---|
88 |
echo $mailFrom |
---|
89 |
} |
---|
90 |
|
---|
91 |
############################################################################### |
---|
92 |
## |
---|
93 |
## |
---|
94 |
## |
---|
95 |
############################################################################### |
---|
96 |
MailSend () { |
---|
97 |
mailEnvFrom=$1 |
---|
98 |
mailEnvToList=$2 |
---|
99 |
msgOrFile=$3 |
---|
100 |
|
---|
101 |
if [ -z "$mailEnvFrom" ] ; then |
---|
102 |
mailEnvFrom=$(MailGetFrom) |
---|
103 |
fi |
---|
104 |
if [ -z "$mailEnvToList" ] ; then |
---|
105 |
mailEnvToList=$(MailGetRecipients) |
---|
106 |
fi |
---|
107 |
if [ -z "$(MailGetAuthName)" -o -z "$(MailGetAuthPasswd)" ] ; then |
---|
108 |
auth=$(MailGetAuthMethod) |
---|
109 |
else |
---|
110 |
auth=off |
---|
111 |
fi |
---|
112 |
|
---|
113 |
wanAddr=$(/sbin/ifconfig eth0 | grep 'inet addr:' | tr ':' ' ' | awk '{print $3;exit}') |
---|
114 |
lanAddr=$(/sbin/ifconfig eth1 | grep 'inet addr:' | tr ':' ' ' | awk '{print $3;exit}') |
---|
115 |
now=$(date '+%Y-%m-%d %H:%M:%S') |
---|
116 |
|
---|
117 |
tmpInFile=/tmp/tmp.MailSend.in.$$ |
---|
118 |
tmpOutFile=/tmp/tmp.MailSend.out.$$ |
---|
119 |
|
---|
120 |
if [ -f "$msgOrFile" ] ; then |
---|
121 |
cp "$msgOrFile" $tmpInFile |
---|
122 |
else |
---|
123 |
echo "$msgOrFile" > $tmpInFile |
---|
124 |
fi |
---|
125 |
|
---|
126 |
for mailEnvTo in $mailEnvToList ; do |
---|
127 |
sed \ |
---|
128 |
-e "s/@mailEnvFrom@/$mailEnvFrom/g" \ |
---|
129 |
-e "s/@mailEnvTo@/$mailEnvTo/g" \ |
---|
130 |
-e "s/@mailRecipients@/$(MailGetRecipients)/g" \ |
---|
131 |
-e "s/@mailAuthName@/$(MailGetAuthName)/g" \ |
---|
132 |
-e "s/@mailAuthPasswd@/$(MailGetAuthPasswd)/g" \ |
---|
133 |
-e "s/@mailAuthMethod@/$(MailGetAuthMethod)/g" \ |
---|
134 |
-e "s/@mailServer@/$(MailGetServer)/g" \ |
---|
135 |
-e "s/@mailPort@/$(MailGetPort)/g" \ |
---|
136 |
-e "s/@mailNotify@/$(MailGetNotify)/g" \ |
---|
137 |
-e "s/@wanAddr@/$wanAddr/g" \ |
---|
138 |
-e "s/@lanAddr@/$lanAddr/g" \ |
---|
139 |
-e "s/@hostname@/$(hostname)/g" \ |
---|
140 |
-e "s/@now@/$now/g" \ |
---|
141 |
< $tmpInFile > $tmpOutFile |
---|
142 |
|
---|
143 |
$msmtp --host="$(MailGetServer)" --port="$(MailGetPort)" \ |
---|
144 |
--auth="$auth" --user="$(MailGetAuthName)" --password="$(MailGetAuthPasswd)" \ |
---|
145 |
--from="$mailEnvFrom" \ |
---|
146 |
$mailEnvTo \ |
---|
147 |
< $tmpOutFile |
---|
148 |
done |
---|
149 |
|
---|
150 |
rm -f $tmpOutFile |
---|
151 |
rm -f $tmpInFile |
---|
152 |
} |
---|
153 |
|
---|
154 |
|
---|
155 |
############################################################################### |
---|
156 |
## |
---|
157 |
## |
---|
158 |
## |
---|
159 |
############################################################################### |
---|
160 |
TestMail() { |
---|
161 |
tmpmsgfile=/tmp/tmp.TestMail.$$ |
---|
162 |
|
---|
163 |
cat > $tmpmsgfile <<-EOF |
---|
164 |
From: @mailEnvFrom@ |
---|
165 |
To: @mailEnvTo@ |
---|
166 |
Subject: Test E-Mail from Thecus N5200 (@hostname@) |
---|
167 |
|
---|
168 |
Hello @mailEnvTo@ |
---|
169 |
|
---|
170 |
This is a test message. |
---|
171 |
|
---|
172 |
mailEnvFrom: '@mailEnvFrom@' |
---|
173 |
mailEnvTo: '@mailEnvTo@' |
---|
174 |
|
---|
175 |
mailRecipients: '@mailRecipients@' |
---|
176 |
mailAuthName: '@mailAuthName@' |
---|
177 |
mailAuthPasswd: '@mailAuthPasswd@' |
---|
178 |
mailAuthMethod: '@mailAuthMethod@' |
---|
179 |
mailServer: '@mailServer@' |
---|
180 |
mailPort: '@mailPort@' |
---|
181 |
mailNotify: '@mailNotify@' |
---|
182 |
|
---|
183 |
wanAddr: '@wanAddr@' |
---|
184 |
lanAddr: '@lanAddr@' |
---|
185 |
|
---|
186 |
hostname: '@hostname@' |
---|
187 |
now: '@now@' |
---|
188 |
|
---|
189 |
======================================================= |
---|
190 |
This is a TEST mail message generated at @now@ on @hostname@. |
---|
191 |
EOF |
---|
192 |
|
---|
193 |
MailSend "" "" $tmpmsgfile |
---|
194 |
rm -f $tmpmsgfile |
---|
195 |
} |
---|