initial working
This commit is contained in:
63
vm1/src/utils/connect/send_email.py
Normal file
63
vm1/src/utils/connect/send_email.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import logging
|
||||
from email.message import EmailMessage
|
||||
|
||||
import aiosmtplib
|
||||
|
||||
from utils.config import loader_email as setting
|
||||
|
||||
cfg = setting.Config()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def send_error_email(unit_name: str, tool_name: str, matlab_cmd: str, matlab_error: str, errors: list, warnings: list) -> None:
|
||||
"""
|
||||
Sends an error email containing details about a MATLAB processing failure.
|
||||
|
||||
The email includes information about the unit, tool, MATLAB command, error message,
|
||||
and lists of specific errors and warnings encountered.
|
||||
|
||||
Args:
|
||||
unit_name (str): The name of the unit involved in the processing.
|
||||
tool_name (str): The name of the tool involved in the processing.
|
||||
matlab_cmd (str): The MATLAB command that was executed.
|
||||
matlab_error (str): The main MATLAB error message.
|
||||
errors (list): A list of detailed error messages from MATLAB.
|
||||
warnings (list): A list of detailed warning messages from MATLAB.
|
||||
"""
|
||||
|
||||
# Creazione dell'oggetto messaggio
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = cfg.subject
|
||||
msg["From"] = cfg.from_addr
|
||||
msg["To"] = cfg.to_addr
|
||||
msg["Cc"] = cfg.cc_addr
|
||||
msg["Bcc"] = cfg.bcc_addr
|
||||
|
||||
MatlabErrors = "<br/>".join(errors)
|
||||
MatlabWarnings = "<br/>".join(dict.fromkeys(warnings))
|
||||
|
||||
# Imposta il contenuto del messaggio come HTML
|
||||
msg.add_alternative(
|
||||
cfg.body.format(
|
||||
unit=unit_name,
|
||||
tool=tool_name,
|
||||
matlab_cmd=matlab_cmd,
|
||||
matlab_error=matlab_error,
|
||||
MatlabErrors=MatlabErrors,
|
||||
MatlabWarnings=MatlabWarnings,
|
||||
),
|
||||
subtype="html",
|
||||
)
|
||||
try:
|
||||
# Use async SMTP to prevent blocking the event loop
|
||||
await aiosmtplib.send(
|
||||
msg,
|
||||
hostname=cfg.smtp_addr,
|
||||
port=cfg.smtp_port,
|
||||
username=cfg.smtp_user,
|
||||
password=cfg.smtp_passwd,
|
||||
start_tls=True,
|
||||
)
|
||||
logger.info("Email inviata con successo!")
|
||||
except Exception as e:
|
||||
logger.error(f"Errore durante l'invio dell'email: {e}")
|
||||
Reference in New Issue
Block a user