Add BCC functionality to email service
- Added optional bcc parameter to EmailOptions interface - Updated sendEmail method to support BCC recipients - Modified notifySubscribersOfNewEntry to use single BCC email instead of individual emails - Improved logging to show BCC recipient count - More efficient email delivery for multiple subscribers
This commit is contained in:
parent
9023d35090
commit
cd835cf31b
1 changed files with 21 additions and 14 deletions
|
|
@ -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<boolean> {
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue