-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.js
69 lines (61 loc) · 2.13 KB
/
skeleton.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
const nodemailer = require("nodemailer");
const fs = require("fs");
const path = require("path");
// Set up the transporter for email
const transporter = nodemailer.createTransport({
service: "<SERVICE>", // e.g., "Gmail"
host: "<SMTP_HOST>", // e.g., "smtp.gmail.com"
port: <SMTP_PORT>, // e.g., 465 for secure or 587 for TLS
secure: true, // Use SSL
auth: {
user: "<EMAIL_USER>", // Your email address
pass: "<EMAIL_PASSWORD>", // Your email password or app-specific password
},
});
// Function to extract the employee name and company from the email address
const extractNamesFromEmail = (email) => {
const [userPart, domainPart] = email.split("@");
const employeeName = userPart
.split(/[._]/)[0]
.replace(/^\w/, (c) => c.toUpperCase());
const companyName = domainPart
.split(".")[0]
.replace(/^\w/, (c) => c.toUpperCase());
return { employeeName, companyName };
};
// Read the file containing email addresses
const emailFilePath = path.join(__dirname, "<EMAIL_FILE>");
fs.readFile(emailFilePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
const emailList = data.split("\n").filter((email) => email.trim() !== "");
const sendEmail = (email, callback) => {
const { employeeName, companyName } = extractNamesFromEmail(email.trim());
const mailOptions = {
from: "<EMAIL_USER>", // Sender address
to: email.trim(), // Receiver email
subject: `<SUBJECT_PLACEHOLDER>`, // Replace with dynamic or static subject
html: `<HTML_BODY_PLACEHOLDER>`, // Replace with email HTML content
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(`Error sending email to ${email}: `, error);
} else {
console.log(`Email sent to ${email}: `, info.response);
}
if (callback) callback();
});
};
const sendEmailsSequentially = (index) => {
if (index < emailList.length) {
sendEmail(emailList[index], () => {
sendEmailsSequentially(index + 1);
});
} else {
console.log("All emails sent!");
}
};
sendEmailsSequentially(0);
});