From d2ab111f908c60305f11341c90809b3c36867b77 Mon Sep 17 00:00:00 2001 From: deajan Date: Thu, 3 Mar 2016 16:33:59 +0100 Subject: [PATCH] Improved mail sending --- dev/debug_obackup.sh | 30 +++++++++++++++++------------- dev/n_obackup.sh | 4 ++-- dev/ofunctions.sh | 26 +++++++++++++++----------- obackup.sh | 30 +++++++++++++++++------------- 4 files changed, 51 insertions(+), 39 deletions(-) diff --git a/dev/debug_obackup.sh b/dev/debug_obackup.sh index 164e488..f7c6963 100755 --- a/dev/debug_obackup.sh +++ b/dev/debug_obackup.sh @@ -5,10 +5,10 @@ PROGRAM="obackup" AUTHOR="(L) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=2.0-pre -PROGRAM_BUILD=2016030301 +PROGRAM_BUILD=2016030302 IS_STABLE=no -## FUNC_BUILD=2016030302 +## FUNC_BUILD=2016030303 ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode @@ -281,9 +281,10 @@ function SendAlert { attachment_command="-a $ALERT_LOG_FILE" fi if type mutt > /dev/null 2>&1 ; then - echo "$MAIL_ALERT_MSG" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mutt) -x -s \"$subject\" $DESTINATION_MAILS $attachment_command" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mutt) !!!" "WARN" + Logger "Cannot send alert email via $(type -p mutt) !!!" "WARN" else Logger "Sent alert mail using mutt." "NOTICE" return 0 @@ -298,12 +299,14 @@ function SendAlert { else attachment_command="" fi - echo "$MAIL_ALERT_MSG" | $(type -p mail) $attachment_command -s "$subject" $DESTINATION_MAILS + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) $attachment_command -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" - echo "$MAIL_ALERT_MSG" | $(type -p mail) -s "$subject" $DESTINATION_MAILS + Logger "Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" + Logger "Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" else Logger "Sent alert mail using mail command without attachment." "NOTICE" return 0 @@ -315,9 +318,10 @@ function SendAlert { fi if type sendmail > /dev/null 2>&1 ; then - echo -e "Subject:$subject\r\n$MAIL_ALERT_MSG" | $(type -p sendmail) $DESTINATION_MAILS + cmd="echo -e \"Subject:$subject\r\n$MAIL_ALERT_MSG\" | $(type -p sendmail) $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendmail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendmail) !!!" "WARN" else Logger "Sent alert mail using sendmail command without attachment." "NOTICE" return 0 @@ -332,7 +336,7 @@ function SendAlert { fi $(type -p sendemail) -f $SENDER_MAIL -t $DESTINATION_MAILS -u "$subject" -m "$MAIL_ALERT_MSG" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1 if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendemail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendemail) !!!" "WARN" else Logger "Sent alert mail using sendemail command without attachment." "NOTICE" return 0 @@ -340,7 +344,7 @@ function SendAlert { fi # If function has not returned 0 yet, assume it's critical that no alert can be sent - Logger "/!\ CRITICAL: Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue + Logger "Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue # Delete tmp log file if [ -f "$ALERT_LOG_FILE" ]; then @@ -996,7 +1000,7 @@ CAN_BACKUP_SQL=1 CAN_BACKUP_FILES=1 function TrapStop { - Logger " /!\ WARNING: Manual exit of backup script. Backups may be in inconsistent state." "WARN" + Logger "/!\ Manual exit of backup script. Backups may be in inconsistent state." "WARN" exit 1 } diff --git a/dev/n_obackup.sh b/dev/n_obackup.sh index a0b035a..2e3a64f 100755 --- a/dev/n_obackup.sh +++ b/dev/n_obackup.sh @@ -5,7 +5,7 @@ PROGRAM="obackup" AUTHOR="(L) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=2.0-pre -PROGRAM_BUILD=2016030301 +PROGRAM_BUILD=2016030302 IS_STABLE=no source "./ofunctions.sh" @@ -27,7 +27,7 @@ CAN_BACKUP_SQL=1 CAN_BACKUP_FILES=1 function TrapStop { - Logger " /!\ WARNING: Manual exit of backup script. Backups may be in inconsistent state." "WARN" + Logger "/!\ Manual exit of backup script. Backups may be in inconsistent state." "WARN" exit 1 } diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index 673042b..adac533 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -1,4 +1,4 @@ -## FUNC_BUILD=2016030302 +## FUNC_BUILD=2016030303 ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode @@ -271,9 +271,10 @@ function SendAlert { attachment_command="-a $ALERT_LOG_FILE" fi if type mutt > /dev/null 2>&1 ; then - echo "$MAIL_ALERT_MSG" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mutt) -x -s \"$subject\" $DESTINATION_MAILS $attachment_command" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mutt) !!!" "WARN" + Logger "Cannot send alert email via $(type -p mutt) !!!" "WARN" else Logger "Sent alert mail using mutt." "NOTICE" return 0 @@ -288,12 +289,14 @@ function SendAlert { else attachment_command="" fi - echo "$MAIL_ALERT_MSG" | $(type -p mail) $attachment_command -s "$subject" $DESTINATION_MAILS + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) $attachment_command -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" - echo "$MAIL_ALERT_MSG" | $(type -p mail) -s "$subject" $DESTINATION_MAILS + Logger "Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" + Logger "Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" else Logger "Sent alert mail using mail command without attachment." "NOTICE" return 0 @@ -305,9 +308,10 @@ function SendAlert { fi if type sendmail > /dev/null 2>&1 ; then - echo -e "Subject:$subject\r\n$MAIL_ALERT_MSG" | $(type -p sendmail) $DESTINATION_MAILS + cmd="echo -e \"Subject:$subject\r\n$MAIL_ALERT_MSG\" | $(type -p sendmail) $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendmail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendmail) !!!" "WARN" else Logger "Sent alert mail using sendmail command without attachment." "NOTICE" return 0 @@ -322,7 +326,7 @@ function SendAlert { fi $(type -p sendemail) -f $SENDER_MAIL -t $DESTINATION_MAILS -u "$subject" -m "$MAIL_ALERT_MSG" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1 if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendemail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendemail) !!!" "WARN" else Logger "Sent alert mail using sendemail command without attachment." "NOTICE" return 0 @@ -330,7 +334,7 @@ function SendAlert { fi # If function has not returned 0 yet, assume it's critical that no alert can be sent - Logger "/!\ CRITICAL: Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue + Logger "Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue # Delete tmp log file if [ -f "$ALERT_LOG_FILE" ]; then diff --git a/obackup.sh b/obackup.sh index e4c0a5c..bb0445a 100755 --- a/obackup.sh +++ b/obackup.sh @@ -5,10 +5,10 @@ PROGRAM="obackup" AUTHOR="(L) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=2.0-pre -PROGRAM_BUILD=2016030301 +PROGRAM_BUILD=2016030302 IS_STABLE=no -## FUNC_BUILD=2016030302 +## FUNC_BUILD=2016030303 ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode @@ -269,9 +269,10 @@ function SendAlert { attachment_command="-a $ALERT_LOG_FILE" fi if type mutt > /dev/null 2>&1 ; then - echo "$MAIL_ALERT_MSG" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mutt) -x -s \"$subject\" $DESTINATION_MAILS $attachment_command" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mutt) !!!" "WARN" + Logger "Cannot send alert email via $(type -p mutt) !!!" "WARN" else Logger "Sent alert mail using mutt." "NOTICE" return 0 @@ -286,12 +287,14 @@ function SendAlert { else attachment_command="" fi - echo "$MAIL_ALERT_MSG" | $(type -p mail) $attachment_command -s "$subject" $DESTINATION_MAILS + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) $attachment_command -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" - echo "$MAIL_ALERT_MSG" | $(type -p mail) -s "$subject" $DESTINATION_MAILS + Logger "Cannot send alert email via $(type -p mail) with attachments !!!" "WARN" + cmd="echo \"$MAIL_ALERT_MSG\" | $(type -p mail) -s \"$subject\" $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" + Logger "Cannot send alert email via $(type -p mail) without attachments !!!" "WARN" else Logger "Sent alert mail using mail command without attachment." "NOTICE" return 0 @@ -303,9 +306,10 @@ function SendAlert { fi if type sendmail > /dev/null 2>&1 ; then - echo -e "Subject:$subject\r\n$MAIL_ALERT_MSG" | $(type -p sendmail) $DESTINATION_MAILS + cmd="echo -e \"Subject:$subject\r\n$MAIL_ALERT_MSG\" | $(type -p sendmail) $DESTINATION_MAILS" + eval $cmd if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendmail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendmail) !!!" "WARN" else Logger "Sent alert mail using sendmail command without attachment." "NOTICE" return 0 @@ -320,7 +324,7 @@ function SendAlert { fi $(type -p sendemail) -f $SENDER_MAIL -t $DESTINATION_MAILS -u "$subject" -m "$MAIL_ALERT_MSG" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1 if [ $? != 0 ]; then - Logger "WARNING: Cannot send alert email via $(type -p sendemail) !!!" "WARN" + Logger "Cannot send alert email via $(type -p sendemail) !!!" "WARN" else Logger "Sent alert mail using sendemail command without attachment." "NOTICE" return 0 @@ -328,7 +332,7 @@ function SendAlert { fi # If function has not returned 0 yet, assume it's critical that no alert can be sent - Logger "/!\ CRITICAL: Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue + Logger "Cannot send alert (neither mutt, mail, sendmail nor sendemail found)." "ERROR" # Is not marked critical because execution must continue # Delete tmp log file if [ -f "$ALERT_LOG_FILE" ]; then @@ -924,7 +928,7 @@ CAN_BACKUP_SQL=1 CAN_BACKUP_FILES=1 function TrapStop { - Logger " /!\ WARNING: Manual exit of backup script. Backups may be in inconsistent state." "WARN" + Logger "/!\ Manual exit of backup script. Backups may be in inconsistent state." "WARN" exit 1 }