LifeAdmin-AI / tools /email_server.py
Maheen001's picture
Update tools/email_server.py
c5990f9 verified
raw
history blame
3.63 kB
from utils.llm_utils import get_llm_response
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pathlib import Path
from datetime import datetime
import json
async def draft_email(
recipient: str,
subject: str,
context: str,
tone: str = 'professional'
) -> dict:
"""
Draft an email based on context
Args:
recipient: Email recipient
subject: Email subject
context: Context/purpose of email
tone: Tone (professional, friendly, formal, casual)
Returns:
Dict with drafted email content
"""
try:
prompt = f"""Draft a {tone} email with the following details:
Recipient: {recipient}
Subject: {subject}
Context/Purpose:
{context}
Write a complete, well-formatted email including:
- Appropriate greeting
- Clear body paragraphs
- Professional closing
- Signature placeholder
Tone should be: {tone}
"""
email_body = await get_llm_response(prompt, temperature=0.7)
# Create formatted email HTML
email_html = f"""<!DOCTYPE html>
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; line-height: 1.6; }}
.email-container {{ max-width: 600px; margin: 20px; }}
.header {{ border-bottom: 2px solid #333; padding-bottom: 10px; }}
.body {{ margin: 20px 0; }}
.footer {{ border-top: 1px solid #ccc; padding-top: 10px; color: #666; }}
</style>
</head>
<body>
<div class="email-container">
<div class="header">
<strong>To:</strong> {recipient}<br>
<strong>Subject:</strong> {subject}
</div>
<div class="body">
{email_body.replace(chr(10), '<br>')}
</div>
<div class="footer">
<em>Drafted by LifeAdmin AI</em>
</div>
</div>
</body>
</html>"""
# Save to file
Path("data/outputs").mkdir(parents=True, exist_ok=True)
output_path = f"data/outputs/email_draft_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html"
with open(output_path, 'w', encoding='utf-8') as f:
f.write(email_html)
# Also save plain text version
txt_path = output_path.replace('.html', '.txt')
with open(txt_path, 'w', encoding='utf-8') as f:
f.write(f"To: {recipient}\nSubject: {subject}\n\n{email_body}")
return {
'success': True,
'output_path_html': output_path,
'output_path_txt': txt_path,
'email_body': email_body,
'recipient': recipient,
'subject': subject
}
except Exception as e:
return {'error': str(e), 'success': False}
async def draft_reply_email(original_email: str, reply_context: str, tone: str = 'professional') -> dict:
"""Draft a reply to an email"""
try:
prompt = f"""Draft a reply to this email:
ORIGINAL EMAIL:
{original_email}
REPLY CONTEXT/INSTRUCTIONS:
{reply_context}
Tone: {tone}
Write a professional reply email."""
reply = await get_llm_response(prompt, temperature=0.7)
Path("data/outputs").mkdir(parents=True, exist_ok=True)
output_path = f"data/outputs/email_reply_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
with open(output_path, 'w', encoding='utf-8') as f:
f.write(reply)
return {
'success': True,
'output_path': output_path,
'reply': reply
}
except Exception as e:
return {'error': str(e), 'success': False}