-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemail_notifier.py
More file actions
161 lines (140 loc) · 5.36 KB
/
Copy pathemail_notifier.py
File metadata and controls
161 lines (140 loc) · 5.36 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
class EmailNotifier:
def __init__(self, config):
self.config = config
self.enabled = config.get('email_notifications', False)
self.email_address = config.get('email_address', '')
self.smtp_server = config.get('smtp_server', 'smtp.gmail.com')
self.smtp_port = config.get('smtp_port', 587)
self.smtp_username = config.get('smtp_username', '')
self.smtp_password = config.get('smtp_password', '')
def send_notification(self, log_id, log_data):
if not self.enabled or not self.email_address:
return
try:
subject = f"PyLoPi Alert: {log_data['error_type']} detected"
html_body = self.create_html_email(log_id, log_data)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.smtp_username
msg['To'] = self.email_address
html_part = MIMEText(html_body, 'html')
msg.attach(html_part)
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
server.login(self.smtp_username, self.smtp_password)
server.send_message(msg)
print(f"Email notification sent for log ID: {log_id}")
except Exception as e:
print(f"Failed to send email notification: {e}")
def create_html_email(self, log_id, log_data):
severity_colors = {
'critical': '#dc3545',
'high': '#fd7e14',
'medium': '#ffc107',
'low': '#28a745'
}
severity_color = severity_colors.get(log_data['severity'], '#6c757d')
html = f'''
<!DOCTYPE html>
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}}
.header {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 5px;
text-align: center;
}}
.severity-badge {{
display: inline-block;
padding: 5px 15px;
background: {severity_color};
color: white;
border-radius: 20px;
font-weight: bold;
margin: 10px 0;
}}
.content {{
background: #f8f9fa;
padding: 20px;
border-radius: 5px;
margin: 20px 0;
}}
.info-row {{
margin: 10px 0;
padding: 10px;
background: white;
border-left: 3px solid #667eea;
}}
.label {{
font-weight: bold;
color: #667eea;
}}
.button {{
display: inline-block;
padding: 12px 30px;
background: #667eea;
color: white;
text-decoration: none;
border-radius: 5px;
margin: 20px 0;
}}
.footer {{
text-align: center;
color: #6c757d;
font-size: 12px;
margin-top: 30px;
}}
</style>
</head>
<body>
<div class="header">
<h1>🔍 PyLoPi Error Alert</h1>
<p>A new error has been detected and analyzed</p>
</div>
<div class="content">
<div class="severity-badge">{log_data['severity'].upper()}</div>
<div class="info-row">
<span class="label">Error Type:</span> {log_data['error_type']}
</div>
<div class="info-row">
<span class="label">Error Message:</span><br>
{log_data['error_message']}
</div>
<div class="info-row">
<span class="label">Log File:</span> {log_data['log_file']}
</div>
<div class="info-row">
<span class="label">Timestamp:</span> {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
</div>
<div class="info-row">
<span class="label">Quick Analysis:</span><br>
{log_data['analysis'][:200]}...
</div>
<center>
<a href="http://localhost:5000/#/log/{log_id}" class="button">
View Full Analysis & Solution
</a>
</center>
</div>
<div class="footer">
<p>This is an automated message from PyLoPi</p>
<p>You can configure notification settings in the PyLoPi dashboard</p>
</div>
</body>
</html>
'''
return html