# Copyright (c) 2026 Stefan Koelle (https://stefankoelle.de) # Licensed under the MIT License. See LICENSE file in project root for details. import logging import smtplib from email.message import EmailMessage from app.config import config log = logging.getLogger("notify") def send_ip_changed_email(old_ip: str | None, new_ip: str) -> bool: if not config.email_notification_enabled: log.debug("E-Mail-Notification nicht konfiguriert, überspringe.") return False if old_ip is None: old_ip = "unbekannt" subject = f"🌐 IP-Update: {old_ip} → {new_ip}" html = f"""\
🔄

IP-Adresse geändert

📍 Alte IP {old_ip}
⬇️
🆕 Neue IP {new_ip}

📡 DynDNS-Updater · Cloudflare + FreeDNS aktualisiert

""" text = f"IP-Adresse geändert: {old_ip} → {new_ip}" msg = EmailMessage() msg["Subject"] = subject msg["From"] = config.notify_email_from msg["To"] = config.notify_email_to msg.set_content(text) msg.add_alternative(html, subtype="html") try: with smtplib.SMTP(config.smtp_host, config.smtp_port) as server: server.ehlo() server.starttls() server.ehlo() server.login(config.smtp_user, config.smtp_password) server.send_message(msg) log.info("E-Mail gesendet: %s -> %s", subject, config.notify_email_to) return True except Exception as exc: log.error("E-Mail-Versand fehlgeschlagen: %s", exc) return False