-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
114 lines (102 loc) · 3.33 KB
/
mailer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const nodemailer = require("nodemailer");
const { google } = require("googleapis");
const fs = require("fs");
const path = require("path");
const { authorize, saveToken } = require("./oauth");
// Load custom variables from environment
const EMAIL = "[email protected]"; // Replace with your email
// Load credentials and token
let credentials, token;
try {
credentials = JSON.parse(fs.readFileSync("client.json"));
token = {};
try {
token = JSON.parse(fs.readFileSync("token.json"));
} catch (e) {
console.log("Token not found");
// Load client secrets from a local file.
fs.readFile("client.json", (err, content) => {
if (err) return console.log("Error loading client secret file:", err);
authorize(JSON.parse(content), saveToken);
});
}
console.log("Credentials and token loaded successfully"); // Debug log
} catch (error) {
console.error("Error loading credentials or token:", error);
process.exit(1);
}
const { client_secret, client_id, redirect_uris } = credentials.installed;
const { refresh_token } = token;
// Create an OAuth2 client
const oauth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
oauth2Client.setCredentials({ refresh_token });
// Load recipients
let mail;
try {
mail = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "recipients.json"))
);
console.log("Recipients loaded:", mail); // Debug log
} catch (error) {
console.error("Error loading recipients:", error);
process.exit(1);
}
async function mailSender() {
try {
const targetEmails = mail.recievers || []; // Note: 'recievers' is misspelled
console.log("Target emails:", targetEmails); // Debug log
let successCount = 0;
let failureCount = 0;
// Get access token
console.log("Getting access token..."); // Debug log
const accessTokenResponse = await oauth2Client.getAccessToken();
const accessToken = accessTokenResponse.token;
console.log("Access token obtained"); // Debug log
// Create reusable transporter object using OAuth2
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: EMAIL,
clientId: client_id,
clientSecret: client_secret,
refreshToken: refresh_token,
accessToken: accessToken,
},
});
// Iterate through target emails and send emails
for (const target of targetEmails) {
if (!target) {
console.log("Skipping empty target"); // Debug log
continue;
}
try {
console.log(`Attempting to send email to ${target.email}`); // Debug log
await transporter.sendMail({
from: `${mail.name} <${mail.email || EMAIL}>`,
to: target.email,
subject: mail.subject.replace("${name}", target.name),
text: "",
html:
mail.body.replace("${name}", target.name) ||
"Please Ignore This Email",
});
successCount++;
console.log(`Email sent successfully to ${target.email}`);
} catch (err) {
console.error(`Failed to send email to ${target.email}`, err);
failureCount++;
}
}
console.log(
`Emails sent: ${successCount}, Failed attempts: ${failureCount}`
);
} catch (err) {
console.error("Error in mailSender function:", err);
}
}
module.exports = mailSender;