mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-04-18 01:52:20 +00:00
Update notification_channels.py
This commit is contained in:
@@ -414,21 +414,65 @@ class EmailChannel(NotificationChannel):
|
||||
|
||||
return self._send_with_retry(_do_send)
|
||||
|
||||
def _send_smtp(self, subject: str, body: str, severity: str,
|
||||
data: Optional[Dict] = None) -> Tuple[int, str]:
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
@staticmethod
|
||||
def _get_logo_path() -> str:
|
||||
"""Locate the ProxMenux logo PNG for email embedding."""
|
||||
import os
|
||||
candidates = [
|
||||
os.path.join(os.path.dirname(__file__), '..', 'public', 'images', 'proxmenux-logo.png'),
|
||||
'/opt/proxmenux-monitor/public/images/proxmenux-logo.png',
|
||||
os.path.join(os.path.dirname(__file__), 'proxmenux-logo.png'),
|
||||
]
|
||||
for p in candidates:
|
||||
real = os.path.realpath(p)
|
||||
if os.path.isfile(real):
|
||||
return real
|
||||
return ''
|
||||
|
||||
def _build_mime_message(self, subject: str, body: str, severity: str,
|
||||
data: Optional[Dict] = None):
|
||||
"""Build a MIMEMultipart email with embedded logo."""
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.image import MIMEImage
|
||||
|
||||
msg = EmailMessage()
|
||||
msg = MIMEMultipart('related')
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = self.from_address
|
||||
msg['To'] = ', '.join(self.to_addresses)
|
||||
msg.set_content(body)
|
||||
|
||||
# Add HTML alternative
|
||||
# Create alternative container (plain text + HTML)
|
||||
alt = MIMEMultipart('alternative')
|
||||
msg.attach(alt)
|
||||
|
||||
# Plain text version
|
||||
alt.attach(MIMEText(body, 'plain', 'utf-8'))
|
||||
|
||||
# HTML version
|
||||
html_body = self._format_html(subject, body, severity, data)
|
||||
if html_body:
|
||||
msg.add_alternative(html_body, subtype='html')
|
||||
alt.attach(MIMEText(html_body, 'html', 'utf-8'))
|
||||
|
||||
# Embed logo as CID attachment
|
||||
logo_path = self._get_logo_path()
|
||||
if logo_path:
|
||||
try:
|
||||
with open(logo_path, 'rb') as f:
|
||||
logo_data = f.read()
|
||||
logo_img = MIMEImage(logo_data, _subtype='png')
|
||||
logo_img.add_header('Content-ID', '<proxmenux-logo>')
|
||||
logo_img.add_header('Content-Disposition', 'inline', filename='proxmenux-logo.png')
|
||||
msg.attach(logo_img)
|
||||
except Exception:
|
||||
pass # Logo not found -- email still works without it
|
||||
|
||||
return msg
|
||||
|
||||
def _send_smtp(self, subject: str, body: str, severity: str,
|
||||
data: Optional[Dict] = None) -> Tuple[int, str]:
|
||||
import smtplib
|
||||
|
||||
msg = self._build_mime_message(subject, body, severity, data)
|
||||
|
||||
server = None
|
||||
try:
|
||||
@@ -479,22 +523,13 @@ class EmailChannel(NotificationChannel):
|
||||
data: Optional[Dict] = None) -> Tuple[int, str]:
|
||||
import os
|
||||
import subprocess
|
||||
from email.message import EmailMessage
|
||||
|
||||
sendmail = '/usr/sbin/sendmail'
|
||||
if not os.path.exists(sendmail):
|
||||
return 0, 'sendmail not found at /usr/sbin/sendmail'
|
||||
|
||||
msg = EmailMessage()
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = self.from_address or 'proxmenux@localhost'
|
||||
msg['To'] = ', '.join(self.to_addresses)
|
||||
msg.set_content(body)
|
||||
|
||||
# Add HTML alternative
|
||||
html_body = self._format_html(subject, body, severity, data)
|
||||
if html_body:
|
||||
msg.add_alternative(html_body, subtype='html')
|
||||
msg = self._build_mime_message(subject, body, severity, data)
|
||||
msg.replace_header('From', self.from_address or 'proxmenux@localhost')
|
||||
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
@@ -606,7 +641,10 @@ class EmailChannel(NotificationChannel):
|
||||
<div style="background:#1f2937;padding:20px 28px;">
|
||||
<table width="100%" cellpadding="0" cellspacing="0" border="0">
|
||||
<tr>
|
||||
<td>
|
||||
<td style="width:40px;vertical-align:middle;padding-right:14px;">
|
||||
<img src="cid:proxmenux-logo" alt="M" width="36" height="36" style="display:block;border:0;border-radius:4px;" />
|
||||
</td>
|
||||
<td style="vertical-align:middle;">
|
||||
<h1 style="margin:0;font-size:18px;font-weight:700;color:#ffffff;letter-spacing:-0.02em;">ProxMenux Monitor</h1>
|
||||
<p style="margin:4px 0 0;font-size:12px;color:#9ca3af;">{html_mod.escape(section_label)} Report</p>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user