Spaces:
Sleeping

DevNumb commited on
Commit
a666207
Β·
verified Β·
1 Parent(s): 2b1bdef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -13
app.py CHANGED
@@ -16,9 +16,9 @@ DEFAULT_REPORT_EMAIL = os.getenv("REPORT_EMAIL")
16
 
17
  # Brevo SMTP Configuration
18
  BREVO_SMTP_SERVER = os.getenv("BREVO_SMTP_SERVER", "smtp-relay.brevo.com")
19
- BREVO_SMTP_PORT = os.getenv("BREVO_SMTP_PORT", "587")
20
- BREVO_SMTP_USERNAME = os.getenv("BREVO_SMTP_USERNAME") # Your Brevo login email
21
- BREVO_SMTP_PASSWORD = os.getenv("BREVO_SMTP_PASSWORD") # Your Brevo SMTP key
22
 
23
  # Store conversation history and settings
24
  conversation_history = []
@@ -53,7 +53,7 @@ def update_report_email(new_email):
53
  return "❌ Please enter a valid email address"
54
 
55
  def send_email_smtp(to_email, subject, html_content, from_name="AI Coordination System"):
56
- """Send email using Brevo SMTP"""
57
  try:
58
  if not BREVO_SMTP_USERNAME or not BREVO_SMTP_PASSWORD:
59
  return "❌ Email not configured - missing Brevo SMTP credentials"
@@ -67,19 +67,47 @@ def send_email_smtp(to_email, subject, html_content, from_name="AI Coordination
67
  # Add HTML content
68
  msg.attach(MIMEText(html_content, 'html'))
69
 
70
- # Create connection to Brevo SMTP
71
- context = ssl.create_default_context()
 
72
 
73
- # Connect to Brevo SMTP server
74
- with smtplib.SMTP(BREVO_SMTP_SERVER, BREVO_SMTP_PORT) as server:
75
- server.starttls(context=context) # Secure the connection
76
- server.login(BREVO_SMTP_USERNAME, BREVO_SMTP_PASSWORD)
77
- server.send_message(msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- return f"βœ… Email sent to {to_email}"
 
 
 
80
 
81
  except Exception as e:
82
- return f"❌ Failed to send email: {str(e)}"
83
 
84
  def analyze_coordination_needs(user_message, department, user_name, image=None):
85
  """Use LLM to analyze coordination needs and suggest actions - with image support"""
 
16
 
17
  # Brevo SMTP Configuration
18
  BREVO_SMTP_SERVER = os.getenv("BREVO_SMTP_SERVER", "smtp-relay.brevo.com")
19
+ BREVO_SMTP_PORT = int(os.getenv("BREVO_SMTP_PORT", "587")) # Convert to integer
20
+ BREVO_SMTP_USERNAME = os.getenv("BREVO_SMTP_USERNAME")
21
+ BREVO_SMTP_PASSWORD = os.getenv("BREVO_SMTP_PASSWORD")
22
 
23
  # Store conversation history and settings
24
  conversation_history = []
 
53
  return "❌ Please enter a valid email address"
54
 
55
  def send_email_smtp(to_email, subject, html_content, from_name="AI Coordination System"):
56
+ """Send email using Brevo SMTP with multiple fallback options"""
57
  try:
58
  if not BREVO_SMTP_USERNAME or not BREVO_SMTP_PASSWORD:
59
  return "❌ Email not configured - missing Brevo SMTP credentials"
 
67
  # Add HTML content
68
  msg.attach(MIMEText(html_content, 'html'))
69
 
70
+ # Try different connection methods
71
+ success = False
72
+ error_messages = []
73
 
74
+ # Method 1: Try TLS on port 587
75
+ try:
76
+ context = ssl.create_default_context()
77
+ with smtplib.SMTP(BREVO_SMTP_SERVER, BREVO_SMTP_PORT) as server:
78
+ server.starttls(context=context)
79
+ server.login(BREVO_SMTP_USERNAME, BREVO_SMTP_PASSWORD)
80
+ server.send_message(msg)
81
+ success = True
82
+ except Exception as e1:
83
+ error_messages.append(f"TLS method failed: {str(e1)}")
84
+
85
+ # Method 2: Try SSL on port 465
86
+ try:
87
+ context = ssl.create_default_context()
88
+ with smtplib.SMTP_SSL(BREVO_SMTP_SERVER, 465, context=context) as server:
89
+ server.login(BREVO_SMTP_USERNAME, BREVO_SMTP_PASSWORD)
90
+ server.send_message(msg)
91
+ success = True
92
+ except Exception as e2:
93
+ error_messages.append(f"SSL method failed: {str(e2)}")
94
+
95
+ # Method 3: Try without TLS (not recommended but might work)
96
+ try:
97
+ with smtplib.SMTP(BREVO_SMTP_SERVER, BREVO_SMTP_PORT) as server:
98
+ server.login(BREVO_SMTP_USERNAME, BREVO_SMTP_PASSWORD)
99
+ server.send_message(msg)
100
+ success = True
101
+ except Exception as e3:
102
+ error_messages.append(f"Plain method failed: {str(e3)}")
103
 
104
+ if success:
105
+ return f"βœ… Email sent to {to_email}"
106
+ else:
107
+ return f"❌ All SMTP methods failed. Errors: {' | '.join(error_messages)}"
108
 
109
  except Exception as e:
110
+ return f"❌ SMTP connection error: {str(e)}"
111
 
112
  def analyze_coordination_needs(user_message, department, user_name, image=None):
113
  """Use LLM to analyze coordination needs and suggest actions - with image support"""