mail scripts : add attachment support, use it for too long mails

This commit is contained in:
Alice Gaudon 2022-09-17 10:23:42 +00:00
parent 8164888ad9
commit d7abf80535
2 changed files with 15 additions and 3 deletions

View File

@ -6,4 +6,10 @@ if [ -f "/root/.host_mail" ]; then
fi fi
SUBJECT=$1 SUBJECT=$1
cat | mailx -s "$SUBJECT" -r "$MAILFROM" "$MAILTO"
ATTACHMENT=""
if [ ! -z "$2" ]; then
ATTACHMENT="-a $2"
fi
cat | mailx -s "$SUBJECT" -r "$MAILFROM" $ATTACHMENT "$MAILTO"

View File

@ -62,7 +62,10 @@ if [ $ERR -ne 0 ] && [ "$ERR" != "$IGNORE_EXIT_CODE" ]; then
echo "Sent" echo "Sent"
else else
echo "Could not send mail, sending safe-body mail..." echo "Could not send mail, sending safe-body mail..."
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Could not send full output, see dead.letter)" | $(dirname $0)/mail.sh "[E+M] $(hostname) $SCRIPT" attachment=$(mktemp log.XXXXXXXXX.txt.zstd)
echo -e "$OUTPUT" | zstd > $attachment
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Too long log was attached as a file)" | $(dirname $0)/mail.sh "[E] $(hostname) $SCRIPT" "$attachment"
rm "$attachment"
echo "Sent" echo "Sent"
fi fi
exit 1 exit 1
@ -74,7 +77,10 @@ elif [ $ALWAYS_SEND -ne 0 ]; then
echo "Sent" echo "Sent"
else else
echo "Could not send mail, sending safe-body mail..." echo "Could not send mail, sending safe-body mail..."
echo -e "Logging execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Could not send full output, see dead.letter)" | $(dirname $0)/mail.sh "[I/M] $(hostname) $SCRIPT" attachment=$(mktemp log.XXXXXXXXX.txt.zstd)
echo -e "$OUTPUT" | zstd > $attachment
echo -e "Logging execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Too long log was attached as a file)" | $(dirname $0)/mail.sh "[I] $(hostname) $SCRIPT" "$attachment"
rm "$attachment"
echo "Sent" echo "Sent"
fi fi
fi fi