-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (63 loc) · 2.34 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { createBodyHTML, createBodyText, createSubject } from "./utils/createMail.js";
import { transporter } from "./utils/nodemailerSetup.js";
import { mails, REPLY_TO_EMAIL, SENDER_NAME } from "./setup.js";
import { SingleBar } from 'cli-progress';
import dotenv from "dotenv";
import fs from "fs";
dotenv.config();
const sendMails = async (mails) => {
try {
const totalMails = mails.length;
let acceptedCount = 0;
let rejectedCount = 0;
let sentCount = 0;
const acceptedEmails = [];
const rejectedEmails = [];
const bar = new SingleBar({
format: 'Sending |{bar}| {percentage}% | {value}/{total} Emails',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
});
bar.start(totalMails, 0);
for await (const [email, placeholders] of mails) {
try {
const info = await transporter.sendMail({
from: `${SENDER_NAME} <${process.env.SMTP_USER}>`,
to: email,
subject: createSubject(placeholders),
text: createBodyText(placeholders),
replyTo: REPLY_TO_EMAIL,
html: createBodyHTML(placeholders),
});
acceptedEmails.push(...info.accepted);
rejectedEmails.push(...info.rejected);
acceptedCount += info.accepted.length;
rejectedCount += info.rejected.length;
sentCount++;
bar.update(sentCount);
} catch (mailError) {
console.error(`Error sending email to ${email}:`, mailError);
rejectedEmails.push(email);
rejectedCount++;
}
}
bar.stop();
console.log("Total Accepted:", acceptedCount);
console.log("Total Rejected:", rejectedCount);
// Log results to log.txt
const logData = [
`Total Accepted: ${acceptedCount}`,
`Total Rejected: ${rejectedCount}`,
'',
'Accepted Emails:',
...acceptedEmails,
'',
'Rejected Emails:',
...rejectedEmails,
].join('\n');
fs.writeFileSync('log.txt', logData, 'utf8');
} catch (err) {
console.error("ERROR:", err);
}
};
sendMails(mails);