Update mailer handling.

This commit is contained in:
Darren 'Tadgy' Austin 2026-04-04 13:06:53 +00:00
commit 007a4dfcc7
2 changed files with 53 additions and 18 deletions

View file

@ -1,17 +1,24 @@
#!/bin/bash
CHECK_DIRS=( '/' '/etc/slackpkg/templates' )
# Default configuration.
CHECK_DIRS=('/')
EMAIL_TO=('sysadmin@slackware.uk')
EMAIL_FROM="\"Server: ${HOSTNAME%%.*}\" <noreply@slackware.uk>"
# Allow /etc/default/warn-git-status to override default configuration.
[[ -e /etc/default/warn-git-status ]] && {
# shellcheck disable=SC1091
source /etc/default/warn-git-status || {
printf "%s: %s\\n" "${0##*/}" "failed reading /etc/default/warn-git-status" >&2
exit 1
}
}
OUTPUT_FILE="/tmp/${0##*/}-$$-$RANDOM"
# Remove the OUTPUT_FILE when done.
trap 'rm -f "$OUTPUT_FILE"' EXIT
# Source the mail configuration.
source /etc/mail.conf "git-status" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2
exit 1
}
# Loop through the list and process.
for DIR in "${CHECK_DIRS[@]}"; do
[[ ! -e "$DIR" ]] || [[ ! -d "$DIR" ]] && continue
@ -20,16 +27,27 @@ for DIR in "${CHECK_DIRS[@]}"; do
unset TMP_OUTPUT
done
# If there's no output, do nothing.
[[ ! -s "$OUTPUT_FILE" ]] && {
exit 0
}
# Send the message.
if [[ -n "${EMAIL_TO[*]}" ]]; then
mailx "${MAILX_ARGS[@]}" -S "from=$EMAIL_FROM" -s "Git statuses" "${EMAIL_TO[@]}" <<<"$(cat "$OUTPUT_FILE")" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2
if hash mailx >/dev/null 2>&1; then
mailx -S "from=$EMAIL_FROM" -s "Mail queue" "${EMAIL_TO[@]}" <"$OUTPUT_FILE" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2
exit 1
}
elif hash mail >/dev/null 2>&1; then
mail -r "$EMAIL_FROM" -s "Mail queue" "${EMAIL_TO[@]}" <"$OUTPUT_FILE" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2
exit 1
}
else
printf "%s: %s\\n" "${0##*/}" "no mailer command found" >&2
exit 1
}
fi
else
printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2
exit 1

View file

@ -1,15 +1,22 @@
#!/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
# Default configuration.
EMAIL_TO=('sysadmin@slackware.uk')
EMAIL_FROM="\"Server: ${HOSTNAME%%.*}\" <noreply@slackware.uk>"
# Allow /etc/default/warn-smtp-queue to override default configuration.
[[ -e /etc/default/warn-smtp-queue ]] && {
# shellcheck disable=SC1091
source /etc/default/warn-smtp-queue || {
printf "%s: %s\\n" "${0##*/}" "failed reading /etc/default/warn-smtp-queue" >&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 ))
sleep $(( 10 + (RANDOM % 30) ))
# Get the queue
TMP_OUTPUT="$(mailq)"
@ -18,10 +25,20 @@ hash mailq 2>/dev/null && {
# 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
if hash mailx >/dev/null 2>&1; then
mailx -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
}
elif hash mail >/dev/null 2>&1; then
mail -r "$EMAIL_FROM" -s "Mail queue" "${EMAIL_TO[@]}" <<<"$TMP_OUTPUT" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2
exit 1
}
else
printf "%s: %s\\n" "${0##*/}" "no mailer command found" >&2
exit 1
}
fi
else
printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2
exit 1