enable multiple to emails SMTP #1061

This commit is contained in:
jokob-sk
2025-05-08 08:12:18 +10:00
parent 0bc93a2352
commit 4d7a55e4f6
6 changed files with 84 additions and 52 deletions

View File

@@ -658,7 +658,7 @@
"description": [
{
"language_code": "en_us",
"string": "Email address to which the notification will be send to."
"string": "Email address to which the notification will be send to. You can enter multiple emails separated by a comma (<code>,</code>)."
},
{
"language_code": "es_es",

View File

@@ -119,44 +119,54 @@ def send(pHTML, pText):
subject, from_email, to_email, message_html, message_text = sanitize_email_content('NetAlertX Report', get_setting_value("SMTP_REPORT_FROM"), get_setting_value("SMTP_REPORT_TO"), pHTML, pText)
# Compose email
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
msg['Date'] = formatdate(localtime=True)
msg.attach (MIMEText (message_text, 'plain'))
msg.attach (MIMEText (message_html, 'html'))
# Set a timeout for the SMTP connection (in seconds)
smtp_timeout = 30
mylog('debug', ['Trying to open connection to ' + str(get_setting_value('SMTP_SERVER')) + ':' + str(get_setting_value('SMTP_PORT'))])
if get_setting_value("LOG_LEVEL") == 'debug':
send_email(msg,smtp_timeout)
emails = []
# handle multiple emails
if ',' in to_email:
emails = [e.strip() for e in to_email.split(',')]
else:
emails.append(to_email.strip())
for mail_addr in emails:
# Compose email
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = mail_addr
msg['Date'] = formatdate(localtime=True)
msg.attach (MIMEText (message_text, 'plain'))
msg.attach (MIMEText (message_html, 'html'))
# Set a timeout for the SMTP connection (in seconds)
smtp_timeout = 30
mylog('debug', ['Trying to open connection to ' + str(get_setting_value('SMTP_SERVER')) + ':' + str(get_setting_value('SMTP_PORT'))])
if get_setting_value("LOG_LEVEL") == 'debug':
try:
send_email(msg,smtp_timeout)
except smtplib.SMTPAuthenticationError as e:
mylog('none', [' ERROR: Couldn\'t connect to the SMTP server (SMTPAuthenticationError)'])
mylog('none', [' ERROR: Double-check your SMTP_USER and SMTP_PASS settings.)'])
mylog('none', [' ERROR: ', str(e)])
except smtplib.SMTPServerDisconnected as e:
mylog('none', [' ERROR: Couldn\'t connect to the SMTP server (SMTPServerDisconnected)'])
mylog('none', [' ERROR: ', str(e)])
except socket.gaierror as e:
mylog('none', [' ERROR: Could not resolve hostname (socket.gaierror)'])
mylog('none', [' ERROR: ', str(e)])
except ssl.SSLError as e:
mylog('none', [' ERROR: Could not establish SSL connection (ssl.SSLError)'])
mylog('none', [' ERROR: Are you sure you need SMTP_FORCE_SSL enabled? Check your SMTP provider docs.'])
mylog('none', [' ERROR: ', str(e)])
else:
try:
send_email(msg,smtp_timeout)
except smtplib.SMTPAuthenticationError as e:
mylog('none', [' ERROR: Couldn\'t connect to the SMTP server (SMTPAuthenticationError)'])
mylog('none', [' ERROR: Double-check your SMTP_USER and SMTP_PASS settings.)'])
mylog('none', [' ERROR: ', str(e)])
except smtplib.SMTPServerDisconnected as e:
mylog('none', [' ERROR: Couldn\'t connect to the SMTP server (SMTPServerDisconnected)'])
mylog('none', [' ERROR: ', str(e)])
except socket.gaierror as e:
mylog('none', [' ERROR: Could not resolve hostname (socket.gaierror)'])
mylog('none', [' ERROR: ', str(e)])
except ssl.SSLError as e:
mylog('none', [' ERROR: Could not establish SSL connection (ssl.SSLError)'])
mylog('none', [' ERROR: Are you sure you need SMTP_FORCE_SSL enabled? Check your SMTP provider docs.'])
mylog('none', [' ERROR: ', str(e)])
# ----------------------------------------------------------------------------------
def send_email(msg,smtp_timeout):

View File

@@ -54,16 +54,27 @@ def check_services_health(site):
mylog('verbose', [f'[{pluginName}] Checking {site}'])
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
try:
resp = requests.get(site, verify=False, timeout=get_setting_value('WEBMON_RUN_TIMEOUT'))
latency = resp.elapsed.total_seconds()
status = resp.status_code
except requests.exceptions.SSLError:
status = 503
except SSLError:
status = 495 # SSL Certificate Error (non-standard, but more meaningful than 503)
latency = 99999
except:
status = 503
mylog('debug', [f'[{pluginName}] SSL error while checking {site}'])
except Timeout:
status = 504 # Gateway Timeout
latency = 99999
mylog('debug', [f'[{pluginName}] Timeout while checking {site}'])
except RequestException as e:
status = 520 # Web server is returning an unknown error (Cloudflare-style)
latency = 99999
mylog('debug', [f'[{pluginName}] Request error while checking {site}: {e}'])
except Exception as e:
status = 500 # Internal Server Error (fallback)
latency = 99999
mylog('debug', [f'[{pluginName}] Unexpected error while checking {site}: {e}'])
mylog('verbose', [f'[{pluginName}] Result for {site} (status|latency) : {status}|{latency}'])