Spaces:
Sleeping
Sleeping
Commit ·
9d3c0c6
1
Parent(s): 65d2d3e
Switch email from SMTP_SSL:465 to STARTTLS:587, restore sender, show failure feedback
Browse files- app/notifier.py +9 -5
- static/js/app.js +7 -3
- templates/index.html +1 -1
app/notifier.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Email notification module.
|
| 3 |
-
Sends HTML-formatted detection reports via SMTP
|
| 4 |
Credentials are read from environment variables — never hardcoded.
|
| 5 |
"""
|
| 6 |
import logging
|
|
@@ -16,8 +16,8 @@ from pathlib import Path
|
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
SMTP_HOST = os.environ.get("SMTP_HOST", "smtp.gmail.com")
|
| 19 |
-
SMTP_PORT = int(os.environ.get("SMTP_PORT", "
|
| 20 |
-
SMTP_USER = os.environ.get("SMTP_USER", "")
|
| 21 |
SMTP_PASS = os.environ.get("SMTP_PASS", "")
|
| 22 |
|
| 23 |
TEMPLATE_PATH = Path(__file__).resolve().parent.parent / "templates" / "ChangeDetection.html"
|
|
@@ -123,11 +123,15 @@ def send_notification(
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
context = ssl.create_default_context()
|
| 126 |
-
with smtplib.
|
|
|
|
|
|
|
|
|
|
| 127 |
server.login(SMTP_USER, SMTP_PASS)
|
| 128 |
server.sendmail(SMTP_USER, recipient, msg.as_string())
|
| 129 |
logger.info("Notification email sent to %s", recipient)
|
| 130 |
return True
|
| 131 |
except Exception as e:
|
| 132 |
-
logger.error("Failed to send notification email: %s",
|
|
|
|
| 133 |
return False
|
|
|
|
| 1 |
"""
|
| 2 |
Email notification module.
|
| 3 |
+
Sends HTML-formatted detection reports via SMTP STARTTLS (port 587).
|
| 4 |
Credentials are read from environment variables — never hardcoded.
|
| 5 |
"""
|
| 6 |
import logging
|
|
|
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
SMTP_HOST = os.environ.get("SMTP_HOST", "smtp.gmail.com")
|
| 19 |
+
SMTP_PORT = int(os.environ.get("SMTP_PORT", "587"))
|
| 20 |
+
SMTP_USER = os.environ.get("SMTP_USER", "vedangofficeserver@gmail.com")
|
| 21 |
SMTP_PASS = os.environ.get("SMTP_PASS", "")
|
| 22 |
|
| 23 |
TEMPLATE_PATH = Path(__file__).resolve().parent.parent / "templates" / "ChangeDetection.html"
|
|
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
context = ssl.create_default_context()
|
| 126 |
+
with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=15) as server:
|
| 127 |
+
server.ehlo()
|
| 128 |
+
server.starttls(context=context)
|
| 129 |
+
server.ehlo()
|
| 130 |
server.login(SMTP_USER, SMTP_PASS)
|
| 131 |
server.sendmail(SMTP_USER, recipient, msg.as_string())
|
| 132 |
logger.info("Notification email sent to %s", recipient)
|
| 133 |
return True
|
| 134 |
except Exception as e:
|
| 135 |
+
logger.error("Failed to send notification email to %s: %s: %s",
|
| 136 |
+
recipient, type(e).__name__, e)
|
| 137 |
return False
|
static/js/app.js
CHANGED
|
@@ -343,9 +343,13 @@ document.getElementById('form-detect')?.addEventListener('submit', async (e) =>
|
|
| 343 |
}
|
| 344 |
const data = await api('POST', '/api/detect', { body: form });
|
| 345 |
showResult(data);
|
| 346 |
-
const
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
showSuccess('dashboard-success', 'Detection complete!' + notifyMsg);
|
| 350 |
loadHistory();
|
| 351 |
} catch (err) {
|
|
|
|
| 343 |
}
|
| 344 |
const data = await api('POST', '/api/detect', { body: form });
|
| 345 |
showResult(data);
|
| 346 |
+
const notifyCbDone = document.getElementById('detect-notify');
|
| 347 |
+
let notifyMsg = '';
|
| 348 |
+
if (notifyCbDone?.checked) {
|
| 349 |
+
notifyMsg = data.notificationSent
|
| 350 |
+
? ' Notification email sent.'
|
| 351 |
+
: ' ⚠ Email notification failed — check SMTP credentials.';
|
| 352 |
+
}
|
| 353 |
showSuccess('dashboard-success', 'Detection complete!' + notifyMsg);
|
| 354 |
loadHistory();
|
| 355 |
} catch (err) {
|
templates/index.html
CHANGED
|
@@ -346,6 +346,6 @@
|
|
| 346 |
</div>
|
| 347 |
</div>
|
| 348 |
|
| 349 |
-
<script src="/static/js/app.js?v=
|
| 350 |
</body>
|
| 351 |
</html>
|
|
|
|
| 346 |
</div>
|
| 347 |
</div>
|
| 348 |
|
| 349 |
+
<script src="/static/js/app.js?v=23"></script>
|
| 350 |
</body>
|
| 351 |
</html>
|