Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 - <hostname>`)
- `SMTP_SERVER` - SMTP relay hostname, used only if the local `mail` command is unavailable (default: unset)
Expand Down
23 changes: 12 additions & 11 deletions remove-old-wordpress-backups/remove-wordpress-backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,32 @@ 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"

[ -n "${EMAIL_TO}" ] || return 0

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

Expand Down
Loading