Add warn-smtp-queue.

This commit is contained in:
Darren 'Tadgy' Austin 2023-11-06 17:20:43 +00:00
commit 85ab7c4b99

31
warn-smtp-queue Executable file
View file

@ -0,0 +1,31 @@
#!/bin/bash
# Source the mail configuration.
source /etc/mail.conf "mail-queue" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2
exit 1
}
# Don't do anything unless 'mailq' is installed.
hash mailq 2>/dev/null && {
# Prevent a race with other cron jobs that produce emails.
sleep $(( RANDOM % 180 ))
# Get the queue
TMP_OUTPUT="$(mailq)"
[[ -z "$TMP_OUTPUT" ]] && exit 0
# Send the message.
if [[ -n "${EMAIL_TO[*]}" ]]; then
mailx "${MAILX_ARGS[@]}" -S "from=$EMAIL_FROM" -s "Mail queue" "${EMAIL_TO[@]}" <<<"$TMP_OUTPUT" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2
exit 1
}
else
printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2
exit 1
fi
}
exit 0