feat(shell-sync): send_notification

This commit is contained in:
hoelee committed 2026-09-02 10:05:00 +08:00
1 parent 8b31447494
commit 9ed38450d8
1 file changed
+43
+43
View File
@@ -93,3 +93,46 @@ validate_settings() {
fi
}
send_notification() {
local subject="$1"
local body="$2"
local config
local -a recipients
if [ "$NOTIFY_MODE" = "none" ]; then
return 0
fi
config="$(mktemp "${PID_DIR}/msmtp.XXXXXX")"
chmod 600 "$config"
{
printf 'defaults\n'
printf 'host %s\n' "$SMTP_HOST"
printf 'port %s\n' "$SMTP_PORT"
printf 'from %s\n' "$SMTP_FROM"
if [ -n "$SMTP_USER" ]; then
printf 'auth on\nuser %s\npassword %s\n' "$SMTP_USER" "$SMTP_PASS"
else
printf 'auth off\n'
fi
case "$SMTP_TLS" in
starttls) printf 'tls on\ntls_starttls on\n' ;;
tls) printf 'tls on\ntls_starttls off\n' ;;
none) printf 'tls off\n' ;;
esac
} > "$config"
IFS=',' read -r -a recipients <<< "$SMTP_TO"
if printf 'From: %s\nTo: %s\nSubject: %s\n\n%s\n' "$SMTP_FROM" "$SMTP_TO" "$subject" "$body" \
| msmtp --file="$config" "${recipients[@]}" >> "$NOTIFICATION_LOG" 2>&1; then
rm -f "$config" "$NOTIFICATION_FAILURE_FILE"
log "Notification sent: $subject"
return 0
fi
rm -f "$config"
printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$subject" > "$NOTIFICATION_FAILURE_FILE"
log "Notification delivery failed; container will remain unhealthy until a notification is sent successfully. Check $NOTIFICATION_LOG"
return 1
}