From 651dec259cdcabb35191b4f54c011037b7d34786 Mon Sep 17 00:00:00 2001 From: AIIrondev Date: Thu, 27 Aug 2026 10:19:47 +0200 Subject: [PATCH] change for the send function --- Website/modules/emailservice/email.py | 33 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/Website/modules/emailservice/email.py b/Website/modules/emailservice/email.py index cfad4f3..ba38114 100644 --- a/Website/modules/emailservice/email.py +++ b/Website/modules/emailservice/email.py @@ -1,5 +1,6 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from email.mime.application import MIMEApplication import smtplib import time import os @@ -21,12 +22,21 @@ def _build_smtp_client(): return smtp -def send(email: list | str, subject: str, text_body: str, html_body: str = None) -> bool: - """Sends the email with both Plain Text and HTML support.""" +def send( + email: list | str, + subject: str, + text_body: str, + html_body: str = None, + attachments: list = None +) -> bool: + """Sends the email with Plain Text, HTML, and optional File Attachments support.""" if isinstance(email, str): email = [email] + if attachments is None: + attachments = [] + TEXT_SIGNATURE = ( "\n\n--\n" "Mit freundlichen Grüßen\n" @@ -72,13 +82,26 @@ def send(email: list | str, subject: str, text_body: str, html_body: str = None) for i, recipient in enumerate(email): start_time = time.time() - msg = MIMEMultipart("alternative") + # Root message structure for email with attachments + msg = MIMEMultipart("mixed") msg["Subject"] = subject msg["From"] = "Invario Team " msg["To"] = str(recipient) - msg.attach(MIMEText(text_content, "plain")) - msg.attach(MIMEText(html_content, "html")) + # Sub-container for Plain Text and HTML bodies + msg_body = MIMEMultipart("alternative") + msg_body.attach(MIMEText(text_content, "plain")) + msg_body.attach(MIMEText(html_content, "html")) + msg.attach(msg_body) + + # Attach files if provided + for file_path in attachments: + if file_path and os.path.isfile(file_path): + filename = os.path.basename(file_path) + with open(file_path, "rb") as file: + part = MIMEApplication(file.read(), Name=filename) + part['Content-Disposition'] = f'attachment; filename="{filename}"' + msg.attach(part) smtp.sendmail( from_addr="no-reply@invario-software.de",