Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
Send health-check email through Mail.app instead of an SMTP password
Jack Rudenko0e00211d74f1
diff --git a/scripts/healthcheck-notify.sh b/scripts/healthcheck-notify.shindex f0a92fad..030db527 100755--- a/scripts/healthcheck-notify.sh+++ b/scripts/healthcheck-notify.sh@@ -14,12 +14,24 @@# Slack incoming webhook:# security add-generic-password -a "$USER" -s claudish-health-slack -w 'https://hooks.slack.com/services/...'#-# Gmail app password, for the email copy (optional):+# Gmail app password, ONLY if you want SMTP instead of Mail.app (optional):# security add-generic-password -a "$USER" -s claudish-health-smtp -w 'your-16-char-app-password'#-# Either credential being absent is not an error — that channel is simply-# skipped and noted in the log. A health check that fails to alert should still-# leave a report behind, so nothing here is allowed to abort the run.+# EMAIL NEEDS NO CREDENTIAL BY DEFAULT. Mail.app on this machine is already+# signed in, so AppleScript hands it the message and it sends as the configured+# account. That is strictly better than storing a second copy of a password:+# nothing to rotate, nothing to leak, and it keeps working when an app password+# is revoked. The SMTP path stays as a fallback for a machine with no Mail.app+# account, and is used only when the keychain item above exists.+#+# ONE-TIME macOS CONSENT: the first time this runs, macOS asks whether the+# controlling process may drive Mail (and Messages). That prompt appears on the+# desktop and must be approved once; until then AppleScript returns error -1743+# and the channel is skipped. Approve it and it never asks again.+#+# A channel with no credential, or with consent denied, is skipped and logged.+# A health check that fails to alert must still leave its report behind, so+# nothing here is allowed to abort the run.set -uREPO="$HOME/mag/claudish"@@ -39,6 +51,14 @@ post_slack() {local text="$1" hookhook="$(kc claudish-health-slack)"if [ -z "$hook" ]; then echo "[notify] no Slack webhook in keychain (claudish-health-slack) — skipped"; return 0; fi+ # Reject an obviously non-real webhook rather than POSTing to it. A real one+ # is https://hooks.slack.com/services/T.../B.../<token> and runs ~75+ chars;+ # a placeholder pasted from documentation hits Slack, gets a 302, and looks+ # like an outage every single morning. Cheaper to catch it here.+ case "$hook" in+ https://hooks.slack.com/services/T*/B*/?*) : ;;+ *) echo "[notify] Slack webhook in keychain is not a real webhook URL — skipped"; return 0 ;;+ esac# --data-binary with a heredoc-built JSON file avoids every quoting trap that# comes with embedding a multi-line report in an inline -d argument.local payload; payload="$(mktemp)"@@ -53,10 +73,33 @@ PYecho "[notify] slack http $code"}+# Preferred email path: hand the message to the Mail.app that is already signed+# in. No password anywhere. Returns non-zero if Mail is unavailable or the+# automation consent has not been granted, so the caller can fall back to SMTP.+send_email_via_mail_app() {+ local subject="$1" body="$2"+ osascript <<APPLESCRIPT 2>&1+tell application "Mail"+ set newMsg to make new outgoing message with properties {subject:"$subject", content:"$body", visible:false}+ tell newMsg to make new to recipient at end of to recipients with properties {address:"$EMAIL_TO"}+ send newMsg+end tell+APPLESCRIPT+}+send_email() {- local subject="$1" body="$2" pass+ local subject="$1" body="$2" pass out+ # AppleScript's `send` returns the literal "true" on success, so an empty+ # result and "true" both mean sent. Anything else is a real error (-1743 is+ # the automation-consent refusal).+ out="$(send_email_via_mail_app "$subject" "$body")"+ if [ -z "$out" ] || [ "$out" = "true" ]; then+ echo "[notify] email sent via Mail.app (no credential needed)"+ return 0+ fi+ echo "[notify] Mail.app path unavailable: ${out:-unknown}"pass="$(kc claudish-health-smtp)"- if [ -z "$pass" ]; then echo "[notify] no SMTP app password in keychain (claudish-health-smtp) — skipped"; return 0; fi+ if [ -z "$pass" ]; then echo "[notify] no SMTP fallback password in keychain — email skipped"; return 0; filocal msg; msg="$(mktemp)"{echo "From: $EMAIL_FROM"@@ -69,7 +112,7 @@ send_email() {curl -sS --url "$SMTP_URL" --ssl-reqd \--mail-from "$EMAIL_FROM" --mail-rcpt "$EMAIL_TO" \--upload-file "$msg" --user "$EMAIL_FROM:$pass" --max-time 40 \- && echo "[notify] email sent" || echo "[notify] email FAILED"+ && echo "[notify] email sent via SMTP" || echo "[notify] email FAILED"rm -f "$msg"}