Files
Key-service-Server/Website/modules/emailservice/email.py
T

169 lines
6.7 KiB
Python

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 = """
<br><br>
<table cellpadding="0" cellspacing="0" border="0" style="font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #555555; line-height: 1.5; border-top: 1px solid #eaebed; padding-top: 15px; width: 100%;">
<tr>
<td>
<p style="margin:0 0 5px 0;">Mit freundlichen Grüßen,</p>
<p style="margin:0;"><strong style="font-size:15px; color: #333333;">Ihr Invario Team</strong></p><br>
<p style="margin:0 0 0 0; font-size: 12px; color: #888888;">
<strong>Invario UG</strong><br>
Am Sportplatz 10<br>
83052 Bruckmühl
</p>
</td>
</tr>
</table>
"""
text_content = f"{text_body}{TEXT_SIGNATURE}"
if html_body:
html_content = f"<html><body style='background-color: #f9f9f9; padding: 20px;'><div style='background-color: #ffffff; padding: 30px; border-radius: 8px; max-width: 600px; margin: 0 auto; box-shadow: 0px 2px 5px rgba(0,0,0,0.05);'>{html_body}{HTML_SIGNATURE}</div></body></html>"
else:
# Fallback if only text is provided
html_safe_text = text_body.replace('\n', '<br>')
html_content = f"<html><body><p style='font-family: Arial, sans-serif; color: #333333;'>{html_safe_text}</p>{HTML_SIGNATURE}</body></html>"
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 <no-reply@invario-software.de>"
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"""
<div style="font-family: Arial, Helvetica, sans-serif; color: #333333;">
<h2 style="color: #2c3e50; margin-top: 0;">Willkommen bei Invario!</h2>
<p style="font-size: 15px; line-height: 1.6;">
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:
</p>
<div style="background-color: #f1f5f9; border-left: 4px solid #3b82f6; border-radius: 4px; padding: 25px; text-align: center; margin: 30px 0;">
<span style="font-size: 32px; font-weight: bold; letter-spacing: 8px; color: #1e293b;">{token}</span>
</div>
<p style="font-size: 13px; color: #64748b; line-height: 1.5;">
Dieser Code ist aus Sicherheitsgründen nur für eine begrenzte Zeit gültig.<br>
Falls Sie kein Konto bei uns erstellt haben, können Sie diese E-Mail sicher ignorieren.
</p>
</div>
"""
return send(email, subject, text_body=text_note, html_body=html_note)
def send_password_reset_token(email: str, token: str) -> bool:
"""Sends a professionally styled password reset token to the user."""
subject = "Aktion erforderlich: Ihr Passwort-Zurücksetzungs-Code für Invario"
text_note = (
"Sie haben eine Anfrage zum Zurücksetzen Ihres Passworts gestellt.\n\n"
"Um Ihr Passwort zurückzusetzen, 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 Anfrage nicht gestellt haben, können Sie diese E-Mail einfach ignorieren."
)
html_note = f"""
<div style="font-family: Arial, Helvetica, sans-serif; color: #333333;">
<h2 style="color: #2c3e50; margin-top: 0;">Passwort zurücksetzen</h2>
<p style="font-size: 15px; line-height: 1.6;">
Sie haben eine Anfrage zum Zurücksetzen Ihres Passworts gestellt. Um fortzufahren, verwenden Sie bitte den folgenden Bestätigungscode:
</p>
<div style="background-color: #f1f5f9; border-left: 4px solid #3b82f6; border-radius: 4px; padding: 25px; text-align: center; margin: 30px 0;">
<span style="font-size: 32px; font-weight: bold; letter-spacing: 8px; color: #1e293b;">{token}</span>
</div>
<p style="font-size: 13px; color: #64748b; line-height: 1.5;">
Dieser Code ist aus Sicherheitsgründen nur für eine begrenzte Zeit gültig.<br>
Falls Sie kein Zurücksetzen des Passworts angefordert haben, können Sie diese E-Mail sicher ignorieren.
</p>
</div>
"""
return send(email, subject, text_body=text_note, html_body=html_note)