#!/bin/sh ############################################################################### ## ## This is a more general script library for sending ## E-Mail on the Thecus N5200. ## ## The main function is MailSend(). ## ## 2007-01-16 Andreas Vogel (omega) ## ## Changes: ## 09.01.2007 0.1 initial version ## 31.05.2007 0.2 change mail from for FW >= 1.00.10 (by Peter Futterknecht (peterfu)) ## ############################################################################### msmtp=/opt/bin/msmtp sqlite=/opt/bin/sqlite confdb=/app/cfg/conf.db MailGetRecipients() { if [ -z "$mailRecipients" ] ; then sqlcmd="select v from conf where k like 'notif_addr%'" mailRecipients=$($sqlite $confdb "$sqlcmd" | tr "\n" " ") fi echo $mailRecipients } MailGetAuthName() { if [ -z "$mailAuthName" ] ; then sqlcmd="select v from conf where k='notif_account'" mailAuthName=$($sqlite $confdb "$sqlcmd") fi echo $mailAuthName } MailGetAuthPasswd() { if [ -z "$mailAuthPasswd" ] ; then sqlcmd="select v from conf where k='notif_password'" mailAuthPasswd=$($sqlite $confdb "$sqlcmd") fi echo $mailAuthPasswd } MailGetAuthMethod() { if [ -z "$mailAuthMethod" ] ; then sqlcmd="select v from conf where k='notif_auth'" mailAuthMethod=$($sqlite $confdb "$sqlcmd") fi echo $mailAuthMethod } MailGetServer() { if [ -z "$mailServer" ] ; then sqlcmd="select v from conf where k='notif_smtp'" mailServer=$($sqlite $confdb "$sqlcmd") fi echo $mailServer } MailGetPort() { if [ -z "$mailPort" ] ; then sqlcmd="select v from conf where k='notif_smtport'" mailPort=$($sqlite $confdb "$sqlcmd") fi echo $mailPort } MailGetNotify() { if [ -z "$mailNotify" ] ; then if [ -n "$1" ];then field="notif_$1" sqlcmd="select v from conf where k='$field'" mailNotify=$($sqlite $confdb "$sqlcmd") fi fi echo $mailNotify } MailGetFrom () { if [ -z "$mailFrom" ] ; then sqlcmd="select v from conf where k='notif_from'" mailFrom=$($sqlite $confdb "$sqlcmd") fi if [ -z "$mailFrom" ] ; then mailFrom="thecus@$(hostname)" fi echo $mailFrom } ############################################################################### ## ## ## ############################################################################### MailSend () { mailEnvFrom=$1 mailEnvToList=$2 msgOrFile=$3 if [ -z "$mailEnvFrom" ] ; then mailEnvFrom=$(MailGetFrom) fi if [ -z "$mailEnvToList" ] ; then mailEnvToList=$(MailGetRecipients) fi if [ -z "$(MailGetAuthName)" -o -z "$(MailGetAuthPasswd)" ] ; then auth=$(MailGetAuthMethod) else auth=off fi wanAddr=$(/sbin/ifconfig eth0 | grep 'inet addr:' | tr ':' ' ' | awk '{print $3;exit}') lanAddr=$(/sbin/ifconfig eth1 | grep 'inet addr:' | tr ':' ' ' | awk '{print $3;exit}') now=$(date '+%Y-%m-%d %H:%M:%S') tmpInFile=/tmp/tmp.MailSend.in.$$ tmpOutFile=/tmp/tmp.MailSend.out.$$ if [ -f "$msgOrFile" ] ; then cp "$msgOrFile" $tmpInFile else echo "$msgOrFile" > $tmpInFile fi for mailEnvTo in $mailEnvToList ; do sed \ -e "s/@mailEnvFrom@/$mailEnvFrom/g" \ -e "s/@mailEnvTo@/$mailEnvTo/g" \ -e "s/@mailRecipients@/$(MailGetRecipients)/g" \ -e "s/@mailAuthName@/$(MailGetAuthName)/g" \ -e "s/@mailAuthPasswd@/$(MailGetAuthPasswd)/g" \ -e "s/@mailAuthMethod@/$(MailGetAuthMethod)/g" \ -e "s/@mailServer@/$(MailGetServer)/g" \ -e "s/@mailPort@/$(MailGetPort)/g" \ -e "s/@mailNotify@/$(MailGetNotify)/g" \ -e "s/@wanAddr@/$wanAddr/g" \ -e "s/@lanAddr@/$lanAddr/g" \ -e "s/@hostname@/$(hostname)/g" \ -e "s/@now@/$now/g" \ < $tmpInFile > $tmpOutFile $msmtp --host="$(MailGetServer)" --port="$(MailGetPort)" \ --auth="$auth" --user="$(MailGetAuthName)" --password="$(MailGetAuthPasswd)" \ --from="$mailEnvFrom" \ $mailEnvTo \ < $tmpOutFile done rm -f $tmpOutFile rm -f $tmpInFile } ############################################################################### ## ## ## ############################################################################### TestMail() { tmpmsgfile=/tmp/tmp.TestMail.$$ cat > $tmpmsgfile <<-EOF From: @mailEnvFrom@ To: @mailEnvTo@ Subject: Test E-Mail from Thecus N5200 (@hostname@) Hello @mailEnvTo@ This is a test message. mailEnvFrom: '@mailEnvFrom@' mailEnvTo: '@mailEnvTo@' mailRecipients: '@mailRecipients@' mailAuthName: '@mailAuthName@' mailAuthPasswd: '@mailAuthPasswd@' mailAuthMethod: '@mailAuthMethod@' mailServer: '@mailServer@' mailPort: '@mailPort@' mailNotify: '@mailNotify@' wanAddr: '@wanAddr@' lanAddr: '@lanAddr@' hostname: '@hostname@' now: '@now@' ======================================================= This is a TEST mail message generated at @now@ on @hostname@. EOF MailSend "" "" $tmpmsgfile rm -f $tmpmsgfile }