-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessEmailsAndSend.js
57 lines (47 loc) · 2.06 KB
/
processEmailsAndSend.js
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
import { sendRegistrationEmail } from "./sendEmail.js";
import fs from 'fs';
import csv from 'csv-parser';
const processAndSend = () => {
const recipients = [];
const batchSize = 50;
const delay = 1000;
// Read the CSV file and extract email addresses
fs.createReadStream('emails.csv')
.pipe(csv())
.on('data', (row) => {
// Convert header keys to lowercase for case-insensitive matching
const normalizedRow = Object.keys(row).reduce((acc, key) => {
acc[key.toLowerCase()] = row[key]; // Normalize keys to lowercase
return acc;
}, {});
// Use lowercase 'email' for accessing the email address
if (normalizedRow.email) {
const email = normalizedRow.email.replace(/"/g, '').trim(); // Clean the email
recipients.push(email); // Add email to the recipients array
console.log(`Email added: ${email}`); // Log the email being added
}
})
.on('end', async () => {
console.log('CSV file successfully processed. Sending emails...');
if (recipients.length === 0) {
console.log('No recipients found in the CSV file.'); // Log if no recipients were found
return; // Exit if there are no recipients
}
// Send emails in batches
for (let i = 0; i < recipients.length; i += batchSize) {
const batch = recipients.slice(i, i + batchSize); // Get the current batch of emails
// Send emails for the current batch
await Promise.all(batch.map(recipient => sendRegistrationEmail(recipient)));
console.log(`Sent ${Math.min(i + batchSize, recipients.length)} of ${recipients.length} emails...`);
// Wait before sending the next batch
if (i + batchSize < recipients.length) {
await new Promise(resolve => setTimeout(resolve, delay));
}
}
console.log('All emails have been sent.');
})
.on('error', (error) => {
console.error('Error reading CSV file:', error); // Log any errors while reading the CSV
});
};
processAndSend(); // Start processing and sending emails