Initial commit: AegisSight-Monitor-Verwaltung
Dieser Commit ist enthalten in:
53
src/email_utils/sender.py
Normale Datei
53
src/email_utils/sender.py
Normale Datei
@@ -0,0 +1,53 @@
|
||||
"""Async E-Mail-Versand via SMTP."""
|
||||
import logging
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
|
||||
import aiosmtplib
|
||||
|
||||
from config import (
|
||||
SMTP_HOST,
|
||||
SMTP_PORT,
|
||||
SMTP_USER,
|
||||
SMTP_PASSWORD,
|
||||
SMTP_FROM_EMAIL,
|
||||
SMTP_FROM_NAME,
|
||||
SMTP_USE_TLS,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("verwaltung.email")
|
||||
|
||||
|
||||
async def send_email(to_email: str, subject: str, html_body: str) -> bool:
|
||||
"""Sendet eine HTML-E-Mail.
|
||||
|
||||
Returns:
|
||||
True bei Erfolg, False bei Fehler.
|
||||
"""
|
||||
if not SMTP_HOST:
|
||||
logger.warning(f"SMTP nicht konfiguriert - E-Mail an {to_email} nicht gesendet: {subject}")
|
||||
return False
|
||||
|
||||
msg = MIMEMultipart("alternative")
|
||||
msg["From"] = f"{SMTP_FROM_NAME} <{SMTP_FROM_EMAIL}>"
|
||||
msg["To"] = to_email
|
||||
msg["Subject"] = subject
|
||||
|
||||
text_content = f"Betreff: {subject}\n\nBitte oeffnen Sie diese E-Mail in einem HTML-faehigen E-Mail-Client."
|
||||
msg.attach(MIMEText(text_content, "plain", "utf-8"))
|
||||
msg.attach(MIMEText(html_body, "html", "utf-8"))
|
||||
|
||||
try:
|
||||
await aiosmtplib.send(
|
||||
msg,
|
||||
hostname=SMTP_HOST,
|
||||
port=SMTP_PORT,
|
||||
username=SMTP_USER if SMTP_USER else None,
|
||||
password=SMTP_PASSWORD if SMTP_PASSWORD else None,
|
||||
start_tls=SMTP_USE_TLS,
|
||||
)
|
||||
logger.info(f"E-Mail gesendet an {to_email}: {subject}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"E-Mail-Versand fehlgeschlagen an {to_email}: {e}")
|
||||
return False
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren