from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib import time def _build_smtp_client(): smtp = smtplib.SMTP( "mail.invario-software.de", 587, timeout=10, ) smtp.ehlo() if True: # Always use TLS for security smtp.starttls() smtp.ehlo() smtp.login("no-reply@invario-software.de", "#,EATwIn,68") 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.""" if isinstance(email, str): email = [email] TEXT_SIGNATURE = ( "\n\n--\n" "Mit freundlichen Grüßen\n" "Ihr Invario Team\n\n" "Invario UG\n" "Am Sportplatz 10\n" "83052 Bruckmühl" ) HTML_SIGNATURE = """

Mit freundlichen Grüßen,

Ihr Invario Team


Invario UG
Am Sportplatz 10
83052 Bruckmühl

""" text_content = f"{text_body}{TEXT_SIGNATURE}" if html_body: html_content = f"
{html_body}{HTML_SIGNATURE}
" else: # Fallback if only text is provided html_safe_text = text_body.replace('\n', '
') html_content = f"

{html_safe_text}

{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"] = subject msg["From"] = "Invario Team " msg["To"] = str(recipient) msg.attach(MIMEText(text_content, "plain")) msg.attach(MIMEText(html_content, "html")) smtp.sendmail( from_addr="no-reply@invario-software.de", 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 def send_register_token(email: str, token: str) -> bool: """Sends a professionally styled registration token to the user.""" subject = "Aktion erforderlich: Ihr Bestätigungscode für Invario" text_note = ( "Herzlich willkommen bei Invario!\n\n" "Um die Einrichtung Ihres Kontos abzuschließen und Ihre E-Mail-Adresse zu bestätigen, " "geben Sie bitte den folgenden Sicherheitscode ein:\n\n" f"Code: {token}\n\n" "Dieser Code ist aus Sicherheitsgründen nur für begrenzte Zeit gültig. " "Falls Sie diese Registrierung nicht angefordert haben, können Sie diese E-Mail einfach ignorieren." ) html_note = f"""

Willkommen bei Invario!

Vielen Dank für Ihre Registrierung. Um die Einrichtung Ihres Kontos abzuschließen und Ihre E-Mail-Adresse zu verifizieren, verwenden Sie bitte den folgenden Bestätigungscode:

{token}

Dieser Code ist aus Sicherheitsgründen nur für eine begrenzte Zeit gültig.
Falls Sie kein Konto bei uns erstellt haben, können Sie diese E-Mail sicher ignorieren.

""" return send(email, subject, text_body=text_note, html_body=html_note)