Jodinho commited on
Commit
d90a168
·
1 Parent(s): d53c03b

fix(diagnose): prioritize Resend check over blocked SMTP sockets

Browse files
Files changed (1) hide show
  1. routes/v1/alerts.py +15 -39
routes/v1/alerts.py CHANGED
@@ -149,31 +149,7 @@ async def diagnose_smtp(to: str = "john.albarka.ibrahim@gmail.com"):
149
  "password_len": len(EMAIL_SMTP_PASSWORD) if EMAIL_SMTP_PASSWORD else 0,
150
  }
151
 
152
- # Test 587
153
- try:
154
- s587 = socket.create_connection((EMAIL_SMTP_HOST, 587), timeout=5)
155
- s587.close()
156
- steps["port_587"] = "CONNECTED"
157
- except Exception as exc:
158
- steps["port_587"] = f"FAILED: {exc}"
159
-
160
- # Test 465 (SSL)
161
- try:
162
- s465 = socket.create_connection((EMAIL_SMTP_HOST, 465), timeout=5)
163
- s465.close()
164
- steps["port_465"] = "CONNECTED"
165
- except Exception as exc:
166
- steps["port_465"] = f"FAILED: {exc}"
167
-
168
- # Test 25
169
- try:
170
- s25 = socket.create_connection((EMAIL_SMTP_HOST, 25), timeout=5)
171
- s25.close()
172
- steps["port_25"] = "CONNECTED"
173
- except Exception as exc:
174
- steps["port_25"] = f"FAILED: {exc}"
175
-
176
- # Test Resend HTTPS dispatch
177
  from config import RESEND_API_KEY, RESEND_FROM_ADDRESS
178
  steps["resend_key_set"] = bool(RESEND_API_KEY)
179
  steps["resend_from"] = RESEND_FROM_ADDRESS
@@ -186,20 +162,20 @@ async def diagnose_smtp(to: str = "john.albarka.ibrahim@gmail.com"):
186
  except Exception as exc:
187
  steps["resend_dispatch"] = f"FAILED: {exc}"
188
 
189
- # If 465 connected, try SSL send
190
- if steps.get("port_465") == "CONNECTED":
191
- try:
192
- with smtplib.SMTP_SSL(EMAIL_SMTP_HOST, 465, timeout=10) as server:
193
- server.login(EMAIL_FROM_ADDRESS, EMAIL_SMTP_PASSWORD)
194
- msg = MIMEText("Pulse AI SSL Diagnostic Test")
195
- msg["From"] = EMAIL_FROM_ADDRESS
196
- msg["To"] = to
197
- msg["Subject"] = "Pulse AI SSL Diagnostic"
198
- server.sendmail(EMAIL_FROM_ADDRESS, to, msg.as_string())
199
- steps["ssl_send_465"] = "SUCCESS"
200
- return JSONResponse(content={"status": "success", "provider": "smtp_465", "steps": steps})
201
- except Exception as exc:
202
- steps["ssl_send_465"] = f"FAILED: {exc}"
203
 
204
  return JSONResponse(content={"status": "error", "steps": steps})
205
 
 
149
  "password_len": len(EMAIL_SMTP_PASSWORD) if EMAIL_SMTP_PASSWORD else 0,
150
  }
151
 
152
+ # Test Resend HTTPS dispatch first (instant over port 443)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  from config import RESEND_API_KEY, RESEND_FROM_ADDRESS
154
  steps["resend_key_set"] = bool(RESEND_API_KEY)
155
  steps["resend_from"] = RESEND_FROM_ADDRESS
 
162
  except Exception as exc:
163
  steps["resend_dispatch"] = f"FAILED: {exc}"
164
 
165
+ # Probe raw SMTP sockets only if Resend is not configured or failed
166
+ try:
167
+ s587 = socket.create_connection((EMAIL_SMTP_HOST, 587), timeout=3)
168
+ s587.close()
169
+ steps["port_587"] = "CONNECTED"
170
+ except Exception as exc:
171
+ steps["port_587"] = f"FAILED: {exc}"
172
+
173
+ try:
174
+ s465 = socket.create_connection((EMAIL_SMTP_HOST, 465), timeout=3)
175
+ s465.close()
176
+ steps["port_465"] = "CONNECTED"
177
+ except Exception as exc:
178
+ steps["port_465"] = f"FAILED: {exc}"
179
 
180
  return JSONResponse(content={"status": "error", "steps": steps})
181