Fix some issues 11

This commit is contained in:
2026-01-26 09:48:32 +01:00
parent a32a90de71
commit 6c50a78dc8
3 changed files with 137 additions and 6 deletions

View File

@@ -47,6 +47,7 @@ class NotificationSettings(BaseServiceSettings):
SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
SMTP_TLS: bool = os.getenv("SMTP_TLS", "true").lower() == "true"
SMTP_SSL: bool = os.getenv("SMTP_SSL", "false").lower() == "true"
SMTP_REQUIRE_AUTH: bool = os.getenv("SMTP_REQUIRE_AUTH", "false").lower() == "true"
# Email Settings
DEFAULT_FROM_EMAIL: str = os.getenv("DEFAULT_FROM_EMAIL", "noreply@bakeryforecast.es")

View File

@@ -37,8 +37,15 @@ class EmailService:
self.smtp_password = settings.SMTP_PASSWORD
self.smtp_tls = settings.SMTP_TLS
self.smtp_ssl = settings.SMTP_SSL
self.smtp_require_auth = settings.SMTP_REQUIRE_AUTH
self.default_from_email = settings.DEFAULT_FROM_EMAIL
self.default_from_name = settings.DEFAULT_FROM_NAME
# Log SMTP configuration mode
if not self.smtp_require_auth or (not self.smtp_user and not self.smtp_password):
logger.info("Email service configured for trusted relay mode (no authentication)")
else:
logger.info("Email service configured with SMTP authentication")
async def send_email(
self,
@@ -287,9 +294,12 @@ class EmailService:
else:
raise starttls_error
# Login only if credentials are provided (optional for trusted relay)
if self.smtp_user and self.smtp_password:
# Login only if required by configuration or if credentials are provided
# This allows for trusted relay mode where authentication is not needed
if self.smtp_require_auth and self.smtp_user and self.smtp_password:
await server.login(self.smtp_user, self.smtp_password)
elif not self.smtp_require_auth:
logger.debug("Skipping SMTP authentication - trusted relay mode")
await server.quit()
logger.info("Email service health check passed")
@@ -327,9 +337,12 @@ class EmailService:
if self.smtp_tls and not self.smtp_ssl:
await server.starttls()
# Login only if credentials are provided (optional for trusted relay)
if self.smtp_user and self.smtp_password:
# Login only if required by configuration or if credentials are provided
# This allows for trusted relay mode where authentication is not needed
if self.smtp_require_auth and self.smtp_user and self.smtp_password:
await server.login(self.smtp_user, self.smtp_password)
elif not self.smtp_require_auth:
logger.debug("Skipping SMTP authentication - trusted relay mode")
# Send email
await server.send_message(message, from_addr=from_email, to_addrs=[to_email])