-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendEmail.js
More file actions
71 lines (64 loc) · 1.89 KB
/
sendEmail.js
File metadata and controls
71 lines (64 loc) · 1.89 KB
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
// Try to load dotenv if available
let dotenvLoaded = false;
try {
require("dotenv").config();
dotenvLoaded = true;
} catch (err) {
console.warn("dotenv package not found. Using default email configuration.");
console.warn(
"To enable email with environment variables, run: npm install dotenv"
);
}
// Try to load nodemailer if available
let nodemailer;
let emailAvailable = false;
try {
nodemailer = require("nodemailer");
emailAvailable = true;
} catch (err) {
console.warn(
"IMPORTANT: nodemailer package not found. Email functionality is disabled."
);
console.warn("To enable email functionality, run: npm install nodemailer");
}
function sendEmail(to, subject, text) {
// If nodemailer is not available, log the message but don't crash the application
if (!emailAvailable) {
console.log("EMAIL NOT SENT (nodemailer missing):");
console.log(` To: ${to}`);
console.log(` Subject: ${subject}`);
console.log(` Text: ${text}`);
return Promise.resolve({
response: "Email not sent - nodemailer not installed",
});
}
// Use environment variables if dotenv loaded, otherwise use defaults
const emailUser = process.env.EMAIL_USER || "databaselibrary7@gmail.com";
const emailPass = process.env.EMAIL_PASS || "jldyebqoowhipxrf";
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: emailUser,
pass: emailPass,
},
});
const mailOptions = {
from: emailUser,
to: to,
subject: subject,
text: text,
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error("Error sending email:", error);
reject(error);
} else {
console.log("Email sent:", info.response);
resolve(info);
}
});
});
}
// Export the function instead of running it directly
module.exports = sendEmail;