1
1
import { ServerClient } from 'postmark'
2
2
// import { renderPaywalledContent, loadContent } from "./content-handlers"; // Note: loadContent might be deprecated, check content-handlers
3
3
import { getContentWithComponentByDirectorySlug } from "./content-handlers" ; // Use this instead of loadContent
4
- import { ExtendedMetadata } from '@/types'
4
+ import { ExtendedMetadata , LeadAnalysis } from '@/types'
5
5
import { emailLogger as logger } from '@/utils/logger' // Import centralized email logger
6
6
import { getContentUrlFromObject } from './content-url'
7
7
@@ -178,7 +178,7 @@ async function extractPreviewContent(productSlug: string): Promise<PreviewConten
178
178
* @returns Postmark response
179
179
*/
180
180
const sendFreeChaptersEmail = async (
181
- input : SendFreeChaptersEmailInput
181
+ input : SendFreeChaptersEmailInput
182
182
) : Promise < MessageSendingResponse | null > => {
183
183
logger . info ( `[START] Sending free chapters email to: ${ input . To } ` ) ;
184
184
logger . debug ( `[CONFIG] Postmark API Key configured: ${ ! ! process . env . POSTMARK_API_KEY } ` ) ;
@@ -268,7 +268,55 @@ You're receiving this email because you requested a preview from Zachary Proser.
268
268
// Handle specific Postmark errors if needed
269
269
// e.g., if (error.code === 406) { ... }
270
270
return null ;
271
- }
271
+ }
272
+ } ;
273
+
274
+ interface SendLeadNotificationInput {
275
+ messages : { role : string ; content : string } [ ] ;
276
+ analysis : LeadAnalysis ;
277
+ email ?: string | null ;
278
+ }
279
+
280
+ const sendLeadNotificationEmail = async (
281
+ input : SendLeadNotificationInput
282
+ ) : Promise < MessageSendingResponse | null > => {
283
+ const adminEmail = process . env . ADMIN_EMAIL || '[email protected] ' ;
284
+
285
+ const conversationText = input . messages
286
+ . map ( ( m ) => `${ m . role } : ${ m . content } ` )
287
+ . join ( '\n' ) ;
288
+
289
+ const emailData = {
290
+
291
+ To : adminEmail ,
292
+ Subject : 'New Potential Lead from Chatbot' ,
293
+ HtmlBody : `
294
+ <h1>New Potential Lead Detected</h1>
295
+ <p><strong>Email:</strong> ${ input . email || 'unknown' } </p>
296
+ <p><strong>Confidence:</strong> ${ input . analysis . confidence } </p>
297
+ <p><strong>Reasons:</strong> ${ input . analysis . reasons . join ( ', ' ) } </p>
298
+ <p><strong>Topics:</strong> ${ input . analysis . topics . join ( ', ' ) } </p>
299
+ <p><strong>Next Steps:</strong> ${ input . analysis . nextSteps . join ( ', ' ) } </p>
300
+ <pre style="white-space:pre-wrap">${ conversationText } </pre>
301
+ ` ,
302
+ TextBody : `New Potential Lead\nEmail: ${ input . email || 'unknown' } \nConfidence: ${ input . analysis . confidence } \nReasons: ${ input . analysis . reasons . join ( ', ' ) } \nTopics: ${ input . analysis . topics . join ( ', ' ) } \nNext Steps: ${ input . analysis . nextSteps . join ( ', ' ) } \nConversation:\n${ conversationText } ` ,
303
+ MessageStream : 'outbound' ,
304
+ } ;
305
+
306
+ try {
307
+ const response = await client . sendEmail ( emailData ) ;
308
+ logger . info ( '[SUCCESS] Lead notification sent:' , response ) ;
309
+ return response ;
310
+ } catch ( error ) {
311
+ logger . error ( '[FAIL] Failed to send lead notification:' , error ) ;
312
+ return null ;
313
+ }
272
314
} ;
273
315
274
- export { sendReceiptEmail , sendFreeChaptersEmail , type SendReceiptEmailInput , type SendFreeChaptersEmailInput } ;
316
+ export {
317
+ sendReceiptEmail ,
318
+ sendFreeChaptersEmail ,
319
+ sendLeadNotificationEmail ,
320
+ type SendReceiptEmailInput ,
321
+ type SendFreeChaptersEmailInput ,
322
+ } ;
0 commit comments