Update app.py
Browse files
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")
|
| 21 |
-
BREVO_SMTP_PASSWORD = os.getenv("BREVO_SMTP_PASSWORD")
|
| 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 |
-
#
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
-
return f"β
|
| 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"""
|