From 75c051a20d5906ede40ae072166d02225bfe22ff Mon Sep 17 00:00:00 2001 From: Aiirondev Date: Sat, 22 Aug 2026 21:02:43 +0200 Subject: [PATCH] implementation of the Email module for further implementation of the register / booking page --- .../static/modules/emailservice/__init__.py | 1 + Website/static/modules/emailservice/email.py | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Website/static/modules/emailservice/__init__.py create mode 100644 Website/static/modules/emailservice/email.py diff --git a/Website/static/modules/emailservice/__init__.py b/Website/static/modules/emailservice/__init__.py new file mode 100644 index 0000000..05ba3b3 --- /dev/null +++ b/Website/static/modules/emailservice/__init__.py @@ -0,0 +1 @@ +# Web.modules.emailservice package initialization diff --git a/Website/static/modules/emailservice/email.py b/Website/static/modules/emailservice/email.py new file mode 100644 index 0000000..70854d9 --- /dev/null +++ b/Website/static/modules/emailservice/email.py @@ -0,0 +1,98 @@ +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import smtplib +import time + +import Web.modules.database.settings as cfg + + +def _build_smtp_client(): + smtp = smtplib.SMTP( + cfg.EMAIL_SMTP_HOST, + cfg.EMAIL_SMTP_PORT, + timeout=cfg.EMAIL_TIMEOUT_SECONDS, + ) + smtp.ehlo() + if cfg.EMAIL_USE_TLS: + smtp.starttls() + smtp.ehlo() + if cfg.EMAIL_USERNAME: + smtp.login(cfg.EMAIL_USERNAME, cfg.EMAIL_PASSWORD or "") + return smtp + + +def send(email: list | str, subject: str, note: str, sender: str) -> bool: + """Sends the email with the link to the Clients.""" + if not cfg.MODULES.is_enabled("mail"): + print("Debug: Module not enabled") + return False + + if isinstance(email, str): + email = [email] + + body_message = note + + HTML_SIGNATURE = f""" + + + + +
+

Mit freundlichen Grüßen

+

Automatisierter Email Verteiler für die Schule: {cfg.get_school_info().get("name")}


+

Invario UG
Am Sportplatz 10
83052 Bruckmühl

+
+ """ + + text_content = f"{body_message}\n\nMit freundlichen Grüßen\n{sender}\n" + + html_content = f""" + + +

{body_message}

+
+ {HTML_SIGNATURE} + + + """ + + mails_per_second = 10 + interval = 1.0 / mails_per_second + + smtp = None + try: + smtp = _build_smtp_client() + + for i, recipient in enumerate(email): + start_time = time.time() + + msg = MIMEMultipart("alternative") + msg["Subject"] = str(subject) + msg["From"] = f"{sender} <{cfg.EMAIL_USERNAME}>" + msg["To"] = str(recipient) + + msg.attach(MIMEText(text_content, "plain")) + msg.attach(MIMEText(html_content, "html")) + + smtp.sendmail( + from_addr=cfg.EMAIL_USERNAME, + to_addrs=[recipient], + msg=msg.as_string() + ) + + elapsed_time = time.time() - start_time + sleep_time = interval - elapsed_time + + if sleep_time > 0 and i < len(email) - 1: + time.sleep(sleep_time) + + return True + except Exception as e: + print(f"Debug: Fehler beim Senden der E-Mail: {e}") + return False + finally: + if smtp: + try: + smtp.quit() + except Exception: + pass \ No newline at end of file