Files
Balzer-WaagenDaten/mail.py
T
Sebastian Serfling 4b40b177c3 Some changes
2025-10-29 11:41:40 +01:00

50 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import datetime
import os
from dotenv import load_dotenv
load_dotenv()
# SMTP-Parameter aus .env lesen
SMTP_SERVER = os.getenv("E_MAIL_SMTP_SERVER")
SMTP_PORT = int(os.getenv("E_MAIL_SMTP_SERVER_PORT"))
SMTP_USER = os.getenv("E_MAIL_ADDRESS")
SMTP_PASS = os.getenv("E_MAIL_ADDRESS_PASSWORD")
MAIL_FROM = os.getenv("E_MAIL_SEND_TO", SMTP_USER)
MAIL_TO = os.getenv("E_MAIL_SEND_TO")
if not all([SMTP_SERVER, SMTP_USER, SMTP_PASS, MAIL_TO]):
print("⚠️ SMTP-Konfiguration unvollständig bitte .env prüfen.")
def send_email(subject: str, body: str):
"""Allgemeine Mail-Funktion"""
msg = MIMEMultipart()
msg["From"] = MAIL_FROM
msg["To"] = MAIL_TO
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain", "utf-8"))
try:
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
print("Inside smtplib")
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(MAIL_FROM, MAIL_TO.split(","), msg.as_string())
print(f"✅ E-Mail gesendet: {subject}")
except Exception as e:
print(f"❌ Fehler beim Senden der E-Mail ({subject}): {e}")
def send_error_email(msg: str, process: str):
"""Fehlermeldung senden"""
subject = f"❌ Fehler im Prozess {process}"
body = f"Prozess: {process}\nZeit: {datetime.datetime.now()}\n\nFehlermeldung:\n{msg}"
send_email(subject, body)
def send_report_email(msg: str, process: str):
"""Erfolgreichen Abschlussbericht senden"""
subject = f"✅ Prozess abgeschlossen: {process}"
body = f"Prozess: {process}\nZeit: {datetime.datetime.now()}\n\nErgebnisbericht:\n{msg}"
send_email(subject, body)