From 85ab7c4b99b1abea021546ef5fa1c623d82153bf Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Mon, 6 Nov 2023 17:20:43 +0000 Subject: [PATCH] Add warn-smtp-queue. --- warn-smtp-queue | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 warn-smtp-queue diff --git a/warn-smtp-queue b/warn-smtp-queue new file mode 100755 index 0000000..150b5d0 --- /dev/null +++ b/warn-smtp-queue @@ -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