Browse Source

Add validation for SMTP username when authentication is enabled

Co-authored-by: cadtoolbox <12723486+cadtoolbox@users.noreply.github.com>
copilot-swe-agent[bot] 3 months ago
parent
commit
efe574591e
1 changed files with 11 additions and 6 deletions
  1. 11 6
      backend/app/services/email_service.py

+ 11 - 6
backend/app/services/email_service.py

@@ -178,24 +178,29 @@ def send_email(
         security = smtp_settings.smtp_security
         auth_enabled = smtp_settings.smtp_auth_enabled
         
+        # Validate username is provided when authentication is enabled
+        if auth_enabled and smtp_settings.smtp_password:
+            if not smtp_settings.smtp_username:
+                raise ValueError("SMTP username is required when authentication is enabled")
+        
         if security == "ssl":
             # Direct SSL connection (typically port 465)
             with smtplib.SMTP_SSL(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
-                if auth_enabled and smtp_settings.smtp_password:
-                    server.login(smtp_settings.smtp_username or "", smtp_settings.smtp_password)
+                if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
+                    server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
                 server.send_message(msg)
         elif security == "starttls":
             # STARTTLS upgrade (typically port 587)
             with smtplib.SMTP(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
                 server.starttls()
-                if auth_enabled and smtp_settings.smtp_password:
-                    server.login(smtp_settings.smtp_username or "", smtp_settings.smtp_password)
+                if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
+                    server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
                 server.send_message(msg)
         else:
             # No encryption (typically port 25) - use with caution
             with smtplib.SMTP(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
-                if auth_enabled and smtp_settings.smtp_password:
-                    server.login(smtp_settings.smtp_username or "", smtp_settings.smtp_password)
+                if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
+                    server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
                 server.send_message(msg)
         logger.info(f"Email sent successfully to {to_email}")
     except Exception as e: