From 068105b1d3c020ba114247ae08d4d73ed61cc006 Mon Sep 17 00:00:00 2001 From: Patrick Lewis <4015312+locus313@users.noreply.github.com> Date: Mon, 24 Aug 2026 16:58:51 -0700 Subject: [PATCH] fix(remove-old-wordpress-backups): prefer SMTP_SERVER over local mail command If SMTP_SERVER is explicitly configured, send the report directly via curl instead of the local 'mail' command. Previously 'mail' was always tried first and only fell back to SMTP if 'mail' was missing entirely, so a misconfigured local MTA (e.g. msmtp CRAM-MD5 auth failures) would block delivery even when a working SMTP relay was configured. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 2 +- .../remove-wordpress-backups.sh | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a0178f4..e26d4cf 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,7 @@ pci-dss-scan\pci-dss-scan.bat https://example.com - `DRY_RUN` - Set to `true` to enable dry-run mode (default: `false`) - Example: `DRY_RUN=true` previews deletions without removing files - `EMAIL_TO` - Email address to receive the per-domain report (default: unset, no email sent) - - Uses the local `mail` command if available; falls back to the SMTP relay settings below via `curl` if not + - If `SMTP_SERVER` is set, sends directly via `curl` (bypassing the local `mail` command entirely); otherwise falls back to the local `mail` command - Report includes, per domain: backups found, backups removed (or would-remove in dry-run), and filenames removed - `EMAIL_SUBJECT` - Subject line for the email report (default: `WordPress Backup Cleanup Report - `) - `SMTP_SERVER` - SMTP relay hostname, used only if the local `mail` command is unavailable (default: unset) diff --git a/remove-old-wordpress-backups/remove-wordpress-backups.sh b/remove-old-wordpress-backups/remove-wordpress-backups.sh index 15395e1..c82d255 100644 --- a/remove-old-wordpress-backups/remove-wordpress-backups.sh +++ b/remove-old-wordpress-backups/remove-wordpress-backups.sh @@ -333,9 +333,10 @@ send_via_smtp() { return "${rc}" } -# Function to email the report when EMAIL_TO is configured. Prefers the local -# 'mail' command; falls back to a direct SMTP relay via curl when no local -# MTA is present but SMTP_SERVER is configured. +# Function to email the report when EMAIL_TO is configured. If SMTP_SERVER is +# explicitly set, it takes priority (sends directly via curl) so an explicit +# relay config always wins over a possibly-misconfigured local MTA. Falls +# back to the local 'mail' command otherwise. send_email_report() { local body="$1" @@ -343,21 +344,21 @@ send_email_report() { local subject="${EMAIL_SUBJECT:-WordPress Backup Cleanup Report - $(hostname -s 2>/dev/null || hostname)}" - if command -v mail >/dev/null 2>&1; then - if echo "${body}" | mail -s "${subject}" "${EMAIL_TO}"; then - log_message "Report emailed to ${EMAIL_TO}" + if [ -n "${SMTP_SERVER}" ]; then + if send_via_smtp "${subject}" "${body}"; then + log_message "Report emailed to ${EMAIL_TO} via SMTP (${SMTP_SERVER}:${SMTP_PORT})" return 0 fi - log_message "WARNING: 'mail' command failed to send report to ${EMAIL_TO}" + log_message "WARNING: Failed to send email report to ${EMAIL_TO} via SMTP (${SMTP_SERVER}:${SMTP_PORT})" return 1 fi - if [ -n "${SMTP_SERVER}" ]; then - if send_via_smtp "${subject}" "${body}"; then - log_message "Report emailed to ${EMAIL_TO} via SMTP (${SMTP_SERVER}:${SMTP_PORT})" + if command -v mail >/dev/null 2>&1; then + if echo "${body}" | mail -s "${subject}" "${EMAIL_TO}"; then + log_message "Report emailed to ${EMAIL_TO}" return 0 fi - log_message "WARNING: Failed to send email report to ${EMAIL_TO} via SMTP (${SMTP_SERVER}:${SMTP_PORT})" + log_message "WARNING: 'mail' command failed to send report to ${EMAIL_TO}" return 1 fi