-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerts.py
More file actions
97 lines (78 loc) · 2.33 KB
/
Copy pathalerts.py
File metadata and controls
97 lines (78 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from datetime import datetime
import smtplib
import ssl
from email.message import EmailMessage
import traceback
from config import EMAIL_ALERTS_ENABLED
def send_error_email(
subject: str,
error: Exception,
context: dict | None = None
):
if not EMAIL_ALERTS_ENABLED:
# Fallback mínimo
print(f"[ALERTA DESACTIVADA] {subject}: {error}")
return
try:
msg = EmailMessage()
msg["From"] = os.getenv("ALERT_EMAIL_FROM")
msg["To"] = os.getenv("ALERT_EMAIL_TO")
msg["Subject"] = subject
body = f"""
Error en CV Assistant
Fecha: {datetime.utcnow().isoformat()} UTC
Tipo: {type(error).__name__}
Mensaje:
{str(error)}
Traceback:
{traceback.format_exc()}
"""
if context:
body += f"\nContexto:\n{context}"
msg.set_content(body)
context_ssl = ssl.create_default_context()
with smtplib.SMTP(
os.getenv("SMTP_HOST"),
int(os.getenv("SMTP_PORT")),
timeout=10
) as server:
server.starttls(context=context_ssl)
server.login(
os.getenv("SMTP_USER"),
os.getenv("SMTP_PASSWORD")
)
server.send_message(msg)
except Exception:
# Nunca romper la app por alertas
pass
def send_email(proposal: str):
if not EMAIL_ALERTS_ENABLED:
print("[EMAIL DESACTIVADO] Propuesta recibida:")
print(proposal)
return
try:
msg = EmailMessage()
msg["From"] = os.getenv("ALERT_EMAIL_FROM")
msg["To"] = os.getenv("ALERT_EMAIL_TO")
msg["Subject"] = "Nueva propuesta recibida en CV_Assistant"
body = f"""Fecha: {datetime.utcnow().isoformat()} UTC
Mensaje:
{proposal}
"""
msg.set_content(body)
context_ssl = ssl.create_default_context()
with smtplib.SMTP(
os.getenv("SMTP_HOST"),
int(os.getenv("SMTP_PORT")),
timeout=10
) as server:
server.starttls(context=context_ssl)
server.login(
os.getenv("SMTP_USER"),
os.getenv("SMTP_PASSWORD")
)
server.send_message(msg)
except Exception:
# Nunca romper la app por alertas
print(f"Exception: {traceback.format_exc()}")