Jodinho commited on
Commit
dd18fae
·
1 Parent(s): 0ab0ad2

feat(alerts): add internal diagnose-smtp endpoint

Browse files
Files changed (1) hide show
  1. routes/v1/alerts.py +43 -0
routes/v1/alerts.py CHANGED
@@ -134,3 +134,46 @@ async def trigger_all_workers(request: Request):
134
  "digests": digest_result,
135
  "cleaned_subscriptions": cleanup_count,
136
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  "digests": digest_result,
135
  "cleaned_subscriptions": cleanup_count,
136
  }
137
+
138
+
139
+ @router.get("/internal/diagnose-smtp")
140
+ async def diagnose_smtp(to: str = "john.albarka.ibrahim@gmail.com"):
141
+ """Diagnostics endpoint: returns exact network and credential state for SMTP."""
142
+ import smtplib
143
+ import socket
144
+ from email.mime.text import MIMEText
145
+ from config import EMAIL_FROM_ADDRESS, EMAIL_SMTP_HOST, EMAIL_SMTP_PASSWORD, EMAIL_SMTP_PORT
146
+
147
+ steps: dict[str, object] = {
148
+ "host": EMAIL_SMTP_HOST,
149
+ "port": EMAIL_SMTP_PORT,
150
+ "from_address": EMAIL_FROM_ADDRESS,
151
+ "password_set": bool(EMAIL_SMTP_PASSWORD),
152
+ "password_len": len(EMAIL_SMTP_PASSWORD) if EMAIL_SMTP_PASSWORD else 0,
153
+ }
154
+
155
+ try:
156
+ sock = socket.create_connection((EMAIL_SMTP_HOST, EMAIL_SMTP_PORT), timeout=10)
157
+ sock.close()
158
+ steps["socket"] = "CONNECTED"
159
+ except Exception as exc:
160
+ steps["socket"] = f"FAILED: {exc}"
161
+ return JSONResponse(content={"status": "error", "steps": steps})
162
+
163
+ try:
164
+ with smtplib.SMTP(EMAIL_SMTP_HOST, EMAIL_SMTP_PORT, timeout=15) as server:
165
+ server.starttls()
166
+ steps["starttls"] = "SUCCESS"
167
+ server.login(EMAIL_FROM_ADDRESS, EMAIL_SMTP_PASSWORD)
168
+ steps["login"] = "SUCCESS"
169
+ msg = MIMEText("Pulse AI SMTP Diagnostic Test")
170
+ msg["From"] = EMAIL_FROM_ADDRESS
171
+ msg["To"] = to
172
+ msg["Subject"] = "Pulse AI SMTP Diagnostic"
173
+ server.sendmail(EMAIL_FROM_ADDRESS, to, msg.as_string())
174
+ steps["sendmail"] = "SUCCESS"
175
+ return JSONResponse(content={"status": "success", "steps": steps})
176
+ except Exception as exc:
177
+ steps["smtp_error"] = f"{type(exc).__name__}: {exc}"
178
+ return JSONResponse(content={"status": "error", "steps": steps})
179
+