File size: 7,658 Bytes
8bab08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
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