cx_ai_agent_v1 / services /personalized_email_generator.py
muzakkirhussain011's picture
Add application files (text files only)
8bab08d
"""
Personalized Email Generator
Creates truly personalized emails by matching CLIENT offerings with PROSPECT needs
"""
import logging
from typing import Dict, Optional
logger = logging.getLogger(__name__)
class PersonalizedEmailGenerator:
"""Generates personalized B2B emails based on client-prospect fit"""
def generate_email(
self,
client_profile: Dict,
prospect_profile: Dict,
contact: Dict
) -> Dict:
"""
Generate highly personalized email
Args:
client_profile: CLIENT company info (what they offer)
prospect_profile: PROSPECT company info (their pain points)
contact: Decision maker contact info (name, title, email)
Returns:
{
'subject': str,
'body': str,
'to': str,
'from': str
}
"""
print(f"\n[EMAIL GEN] Generating personalized email")
print(f"[EMAIL GEN] Client: {client_profile.get('name')}")
print(f"[EMAIL GEN] Prospect: {prospect_profile.get('name')}")
print(f"[EMAIL GEN] Contact: {contact.get('name')} ({contact.get('title')})")
# Extract key information
client_name = client_profile.get('name', 'Our Company')
prospect_name = prospect_profile.get('name', 'Your Company')
contact_name = contact.get('name', 'Hello')
contact_title = contact.get('title', 'Team Member')
contact_email = contact.get('email', 'contact@example.com')
# Get first name for greeting
first_name = contact_name.split()[0] if contact_name and contact_name != 'Hello' else 'there'
# Get client offerings
client_offerings = client_profile.get('offerings', [])
client_value_props = client_profile.get('value_propositions', [])
client_description = client_profile.get('description', '')
# Get prospect details
prospect_pains = prospect_profile.get('pain_points', [])
prospect_industry = prospect_profile.get('industry', 'your industry')
prospect_size = prospect_profile.get('size', 'your company size')
# Create subject line
subject = self._generate_subject(
client_name,
prospect_name,
prospect_industry,
contact_title
)
# Create personalized body
body = self._generate_body(
client_name=client_name,
client_description=client_description,
client_offerings=client_offerings,
client_value_props=client_value_props,
prospect_name=prospect_name,
prospect_industry=prospect_industry,
prospect_size=prospect_size,
prospect_pains=prospect_pains,
contact_first_name=first_name,
contact_title=contact_title
)
# Determine from address
client_domain = client_profile.get('domain', 'example.com')
from_email = f"partnerships@{client_domain}" if client_domain else "partnerships@example.com"
result = {
'subject': subject,
'body': body,
'to': contact_email,
'from': from_email,
'client': client_name,
'prospect': prospect_name,
'contact': contact_name
}
print(f"[EMAIL GEN] Subject: {subject}")
print(f"[EMAIL GEN] To: {contact_email}")
print(f"[EMAIL GEN] Email generated successfully\n")
return result
def _generate_subject(
self,
client_name: str,
prospect_name: str,
industry: str,
title: str
) -> str:
"""Generate personalized subject line"""
# Subject templates based on title
if 'CEO' in title or 'Founder' in title:
return f"{prospect_name} + {client_name}: Scaling {industry} Operations"
elif 'VP' in title or 'Chief' in title:
return f"Helping {prospect_name} Improve Customer Experience"
elif 'Director' in title or 'Head' in title:
return f"Quick question about {prospect_name}'s customer strategy"
else:
return f"{prospect_name}: Customer Experience Partnership"
def _generate_body(
self,
client_name: str,
client_description: str,
client_offerings: list,
client_value_props: list,
prospect_name: str,
prospect_industry: str,
prospect_size: str,
prospect_pains: list,
contact_first_name: str,
contact_title: str
) -> str:
"""Generate personalized email body"""
# Opening - personalized greeting
opening = f"Hi {contact_first_name},"
# Introduction - acknowledge their role
if 'CEO' in contact_title or 'Founder' in contact_title:
intro = f"\n\nAs {contact_title} of {prospect_name}, you're likely focused on scaling operations while maintaining quality."
elif 'VP' in contact_title or 'Chief' in contact_title:
intro = f"\n\nAs {contact_title}, you're probably looking for ways to improve {prospect_name}'s customer experience and operational efficiency."
else:
intro = f"\n\nI wanted to reach out regarding {prospect_name}'s customer experience strategy."
# Client intro - what they do
if client_description:
client_intro = f"\n\n{client_name} {client_description[:200]}"
elif client_offerings:
client_intro = f"\n\n{client_name} provides {client_offerings[0][:150]}"
else:
client_intro = f"\n\n{client_name} helps {prospect_industry} companies improve their operations."
# Value proposition - match with prospect needs
value_section = "\n\nWe've helped similar companies"
if prospect_pains and len(prospect_pains) > 0:
# Use actual pain points
main_pain = prospect_pains[0]
value_section += f" tackle challenges like:\n• {main_pain}"
# Add more if available
if len(prospect_pains) > 1:
value_section += f"\n• {prospect_pains[1]}"
elif client_value_props:
# Use client value props
value_section += ":\n"
for prop in client_value_props[:2]:
if len(prop) < 150:
value_section += f"• {prop}\n"
else:
# Generic fallback
value_section += " improve customer satisfaction and operational efficiency."
# Call to action
if prospect_size and 'employee' in str(prospect_size).lower():
cta = f"\n\nGiven {prospect_name}'s size and growth trajectory, I think there might be a good fit here."
else:
cta = f"\n\nI think there might be a good fit between {client_name} and {prospect_name}."
cta += f"\n\nWould you have 15 minutes next week for a quick call to explore if we can help {prospect_name}?"
# Sign off
signoff = f"\n\nBest regards,\n{client_name} Partnerships Team"
# Footer
footer = f"\n\n---\nThis email was sent on behalf of {client_name}.\nTo unsubscribe, reply with \"unsubscribe\"."
# Combine all parts
body = opening + intro + client_intro + value_section + cta + signoff + footer
return body
# Singleton
_email_generator = None
def get_email_generator() -> PersonalizedEmailGenerator:
"""Get singleton instance"""
global _email_generator
if _email_generator is None:
_email_generator = PersonalizedEmailGenerator()
return _email_generator