Fix some issues 10

This commit is contained in:
2026-01-26 07:57:18 +01:00
parent dc786eae2e
commit 6e60956613
3 changed files with 22 additions and 29 deletions

View File

@@ -72,9 +72,7 @@ class EmailService:
logger.info("Email notifications disabled")
return True # Return success to avoid blocking workflow
if not self.smtp_user or not self.smtp_password:
logger.error("SMTP credentials not configured")
return False
# Note: Authentication is optional for trusted relay (e.g., internal Mailu with subnet trust)
# Validate email address
if not to_email or "@" not in to_email:
@@ -270,34 +268,28 @@ class EmailService:
if not settings.ENABLE_EMAIL_NOTIFICATIONS:
return True # Service is "healthy" if disabled
if not self.smtp_user or not self.smtp_password:
logger.warning("SMTP credentials not configured")
return False
# Test SMTP connection
if self.smtp_ssl:
# Use implicit TLS/SSL connection (port 465 typically)
server = aiosmtplib.SMTP(hostname=self.smtp_host, port=self.smtp_port, use_tls=True)
await server.connect()
# No need for starttls() when using implicit TLS
else:
# Use plain connection, optionally upgrade with STARTTLS
server = aiosmtplib.SMTP(hostname=self.smtp_host, port=self.smtp_port)
await server.connect()
if self.smtp_tls:
# Try STARTTLS, but handle case where connection is already secure
try:
await server.starttls()
except Exception as starttls_error:
# If STARTTLS fails because connection is already using TLS, that's okay
if "already using TLS" in str(starttls_error) or "already secure" in str(starttls_error):
logger.debug("SMTP connection already secure, skipping STARTTLS")
else:
# Re-raise other STARTTLS errors
raise starttls_error
await server.login(self.smtp_user, self.smtp_password)
# Login only if credentials are provided (optional for trusted relay)
if self.smtp_user and self.smtp_password:
await server.login(self.smtp_user, self.smtp_password)
await server.quit()
logger.info("Email service health check passed")
@@ -335,8 +327,9 @@ class EmailService:
if self.smtp_tls and not self.smtp_ssl:
await server.starttls()
# Login
await server.login(self.smtp_user, self.smtp_password)
# Login only if credentials are provided (optional for trusted relay)
if self.smtp_user and self.smtp_password:
await server.login(self.smtp_user, self.smtp_password)
# Send email
await server.send_message(message, from_addr=from_email, to_addrs=[to_email])