""" Email notification service for OPC """ import logging import os import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText logger = logging.getLogger(__name__) # Email configuration from environment SMTP_HOST = os.getenv("SMTP_HOST", "smtp.gmail.com") SMTP_PORT = int(os.getenv("SMTP_PORT", "587")) SMTP_USER = os.getenv("SMTP_USER", "") SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "") SMTP_FROM = os.getenv("SMTP_FROM", SMTP_USER) SMTP_TO = os.getenv("SMTP_TO", "") async def send_email(subject: str, body: str, html_body: str = None): """Send email notification""" if not SMTP_USER or not SMTP_PASSWORD or not SMTP_TO: logger.warning("Email not configured, skipping notification") return try: # Create message msg = MIMEMultipart("alternative") msg["Subject"] = subject msg["From"] = SMTP_FROM msg["To"] = SMTP_TO # Add plain text part msg.attach(MIMEText(body, "plain")) # Add HTML part if provided if html_body: msg.attach(MIMEText(html_body, "html")) # Send email with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server: server.starttls() server.login(SMTP_USER, SMTP_PASSWORD) server.send_message(msg) logger.info(f"Email sent: {subject}") except Exception as e: logger.error(f"Failed to send email: {e}") async def send_task_assigned_email(task: dict, assigned_to: str, assigned_type: str): """Send email when task is assigned""" subject = f"Task Assigned: {task['title']}" body = f"""You have been assigned a new task: Title: {task['title']} Priority: {task['priority']} Status: {task['status']} Description: {task.get('description', 'No description')} View task: https://nas.jimmygan.com/?page=opc """ html_body = f"""

Task Assigned

You have been assigned a new task:

Title:{task['title']}
Priority:{task['priority']}
Status:{task['status']}

Description:

{task.get('description', 'No description')}

View Task

""" await send_email(subject, body, html_body) async def send_agent_approval_email(agent_name: str, task: dict, reasoning: str, actions: list): """Send email for agent approval request""" subject = f"Agent Approval Required: {agent_name}" actions_text = "\n".join( [f"- {action.get('type')}: {action.get('title', action.get('message', 'N/A'))}" for action in actions] ) body = f"""Agent approval required: Agent: {agent_name} Task: {task['title']} Reasoning: {reasoning} Proposed Actions: {actions_text} Please review and approve in the OPC dashboard: https://nas.jimmygan.com/?page=opc """ html_body = f"""

🤖 Agent Approval Required

Agent: {agent_name}

Task: {task['title']}

Reasoning:

{reasoning}

Proposed Actions:

Review & Approve

""" await send_email(subject, body, html_body) async def send_agent_completion_email(agent_name: str, task: dict, actions_count: int): """Send email when agent completes task""" subject = f"Agent Task Completed: {agent_name}" body = f"""Agent has completed a task: Agent: {agent_name} Task: {task['title']} Actions Executed: {actions_count} View details: https://nas.jimmygan.com/?page=opc """ html_body = f"""

✅ Agent Task Completed

Agent: {agent_name}

Task: {task['title']}

Actions Executed: {actions_count}

View Details

""" await send_email(subject, body, html_body)