import os import base64 from datetime import datetime import pytz import sib_api_v3_sdk from sib_api_v3_sdk.rest import ApiException ### Email Utility Functions ### def load_email_template(): """Load the HTML email template""" try: template_path = 'src/email_template/email_template.html' with open(template_path, 'r', encoding='utf-8') as file: return file.read() except FileNotFoundError: print("Email template file not found.") return None def send_email(to_email, subject, html_content, to_name="Valued Customer", cc_list=None): """Send email using the Brevo (Sendinblue) API""" try: api_key = os.getenv("BREVO_API_KEY") if not api_key: raise Exception("BREVO_API_KEY environment variable not set.") configuration = sib_api_v3_sdk.Configuration() configuration.api_key["api-key"] = api_key api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration)) sender = {"name": "Blue Bean Data", "email": "info@bluebeandata.com"} to = [{"email": to_email, "name": to_name}] cc = [] if cc_list: for cc_email in cc_list: cc.append({"email": cc_email}) send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( to=to, sender=sender, subject=subject, html_content=html_content, cc=cc if cc else None ) try: response = api_instance.send_transac_email(send_smtp_email) return True except ApiException as e: print(f"Brevo API Exception: {e}") return False except Exception as e: print(f"Error sending email with Brevo: {str(e)}") return False def _embed_logo_in_template(template): """Replaces the logo placeholder with a web-hosted image URL.""" logo_url = "https://huggingface.co/spaces/krinya/bluebeandatachatbot/resolve/main/src/email_template/bluebeanlogo.png" return template.replace("__LOGO_SRC__", logo_url) def send_user_welcome_email(email, name="Valued Customer", notes=""): """Send a welcome email to the user and a notification to the company.""" try: template = load_email_template() if not template: return False template_with_logo = _embed_logo_in_template(template) # Handle name display - use generic greeting if no name provided if not name or name.strip().lower() in ['name not provided', 'not provided', '']: greeting = "Greetings," display_name = "Valued Customer" else: greeting = f"Dear {name}," display_name = name # Build user info section dynamically user_info_items = [f"
{greeting}
Thank you for your interest in Blue Bean Data! We're excited to connect with you.
Our team specializes in:
We've received your details and will be in touch shortly. For your records, here is the information you provided:
Best regards,
The Blue Bean Data Team
The AI assistant was unable to answer the following question:
"{question}"
This might be an opportunity to update the knowledge base or add new content to better serve future inquiries.
Timestamp: {timestamp}
Best regards,
Blue Bean Data AI Assistant