Use context manager for SMTP

This commit is contained in:
2026-01-23 19:40:58 +01:00
parent 6c4b876191
commit f0b924ea56

View File

@@ -76,11 +76,10 @@ class EmailService:
smtp_host = self.config["smtp_host"]
smtp_port = self.config["smtp_port"]
server = smtplib.SMTP(smtp_host, smtp_port)
if self.config.get("smtp_starttls", False):
server.starttls()
with smtplib.SMTP(smtp_host, smtp_port) as server:
if self.config.get("smtp_starttls", False):
server.starttls()
try:
if self.config.get("smtp_user"):
server.login(
self.config["smtp_user"],
@@ -89,8 +88,6 @@ class EmailService:
server.sendmail(msg["From"], [to], msg.as_string())
logging.info(f"Email sent: to={to} subject={subject}")
return True
finally:
server.quit()
except Exception as e:
logging.error(f"Email send failed: to={to} error={e}")