feat(shell-sync): send_notification
This commit is contained in:
1 parent
78e373b12a
commit
0cc77c0256
1 file changed
+43
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user