diff --git a/backend/src/services/emailService.ts b/backend/src/services/emailService.ts index d01826de..d60412b8 100644 --- a/backend/src/services/emailService.ts +++ b/backend/src/services/emailService.ts @@ -3,6 +3,7 @@ import nodemailer from 'nodemailer'; interface EmailOptions { to: string; + bcc?: string[]; subject: string; html: string; } @@ -45,7 +46,7 @@ export class EmailService { async sendEmail(options: EmailOptions): Promise { try { - const mailOptions = { + const mailOptions: any = { from: { name: 'Court Docket System', address: process.env['GOOGLE_EMAIL'] || 'system@deafgain.org', @@ -55,10 +56,16 @@ export class EmailService { html: options.html, }; + // Add BCC if provided + if (options.bcc && options.bcc.length > 0) { + mailOptions.bcc = options.bcc; + } + const result = await this.transporter.sendMail(mailOptions); logger.info('Email sent successfully:', { to: options.to, + bcc: options.bcc ? `${options.bcc.length} recipients` : 'none', subject: options.subject, messageId: result.messageId, }); @@ -85,21 +92,21 @@ export class EmailService { const subject = `New Court Filing: ${entryTitle}`; const html = this.generateNotificationEmail(entryTitle, entryDate, entrySummary); + const subscriberEmails = subscribers.map((subscriber: any) => subscriber.email); - // Send emails to all subscribers - const emailPromises = subscribers.map((subscriber: any) => - this.sendEmail({ - to: subscriber.email, - subject, - html, - }) - ); + // Send single email with all subscribers as BCC + const success = await this.sendEmail({ + to: process.env['GOOGLE_EMAIL'] || 'system@deafgain.org', + bcc: subscriberEmails, + subject, + html, + }); - const results = await Promise.allSettled(emailPromises); - const successful = results.filter((result: any) => result.status === 'fulfilled').length; - const failed = results.filter((result: any) => result.status === 'rejected').length; - - logger.info(`Email notifications sent: ${successful} successful, ${failed} failed`); + if (success) { + logger.info(`Email notification sent successfully to ${subscriberEmails.length} subscribers via BCC`); + } else { + logger.error(`Failed to send BCC email notification to ${subscriberEmails.length} subscribers`); + } } catch (error) { logger.error('Failed to notify subscribers:', error); }