improve: test connection send test email to self after auth passes

linuztx committed Mar 18, 2026 at 20:00 UTC be2587e815aa8cb25147892984a642d97ac8517d
1 file changed +36 -9
plugins/_email_integration/api/test_connection.py
+36 -9
@@ -8,7 +8,7 @@ from plugins._email_integration.helpers.imap_client import (
8 disconnect_imap,
9 get_highest_uid,
10 )
11 -from plugins._email_integration.helpers.smtp_client import SmtpConfig, test_smtp
11 +from plugins._email_integration.helpers.smtp_client import SmtpConfig, test_smtp, send_reply
12
13
14 class TestConnection(ApiHandler):
@@ -21,6 +21,8 @@ class TestConnection(ApiHandler):
21 if account_type == "imap":
22 await self._test_imap(handler, results)
23 await self._test_smtp(handler, results)
24 + if all(r["ok"] for r in results):
25 + await self._test_send(handler, results)
26
27 ok = all(r["ok"] for r in results)
28 return {"success": ok, "results": results}
@@ -47,16 +49,18 @@ class TestConnection(ApiHandler):
49 "message": format_error(e),
50 })
51
52 + def _smtp_config(self, handler: dict) -> SmtpConfig:
53 + smtp_server = handler.get("smtp_server") or handler.get("imap_server", "")
54 + return SmtpConfig(
55 + server=smtp_server,
56 + port=int(handler.get("smtp_port", 587)),
57 + username=handler.get("username", ""),
58 + password=handler.get("password", ""),
59 + )
60 +
61 async def _test_smtp(self, handler: dict, results: list[dict]):
62 try:
52 - smtp_server = handler.get("smtp_server") or handler.get("imap_server", "")
53 - cfg = SmtpConfig(
54 - server=smtp_server,
55 - port=int(handler.get("smtp_port", 587)),
56 - username=handler.get("username", ""),
57 - password=handler.get("password", ""),
58 - )
59 - error = await test_smtp(cfg)
63 + error = await test_smtp(self._smtp_config(handler))
64 if error:
65 results.append({"test": "SMTP", "ok": False, "message": error})
66 else:
@@ -71,3 +75,26 @@ class TestConnection(ApiHandler):
75 "ok": False,
76 "message": format_error(e),
77 })
78 +
79 + async def _test_send(self, handler: dict, results: list[dict]):
80 + try:
81 + error = await send_reply(
82 + config=self._smtp_config(handler),
83 + to=handler.get("username", ""),
84 + subject="Agent Zero - Connection Test",
85 + body="This is a test email from Agent Zero email integration.",
86 + )
87 + if error:
88 + results.append({"test": "Send", "ok": False, "message": error})
89 + else:
90 + results.append({
91 + "test": "Send",
92 + "ok": True,
93 + "message": "Test email sent to self",
94 + })
95 + except Exception as e:
96 + results.append({
97 + "test": "Send",
98 + "ok": False,
99 + "message": format_error(e),
100 + })