-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (50 loc) · 1.48 KB
/
Copy pathserver.js
File metadata and controls
59 lines (50 loc) · 1.48 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
require("dotenv").config();
const express = require("express");
const nodemailer = require("nodemailer");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors('*'));
app.use(bodyParser.json());
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PASSWORD,
},
});
app.post("/mail", async (req, res) => {
const { email, subject, text, name, attachments } = req.body;
if (!email) {
return res.status(400).json({ message: "Email is required" });
}
// if(att)
if(attachments && attachments.length > 0){
attachments.forEach((attachment) => {
if (!attachment.path) {
return res.status(500).json({ message: "Invalid attachment" });
}
else if(!attachment.filename) {
attachment.filename = attachment.path.split("/").pop();
}
});
}
try {
const mailOptions = {
from: process.env.EMAIL,
to: email,
subject: subject + (name? " - " + name : ""),
text: text,
attachments: attachments? attachments : [],
};
await transporter.sendMail(mailOptions);
res.status(200).json({ message: "Email sent successfully" });
} catch (error) {
console.error("Error sending email:", error);
res.status(500).json({ message: "Failed to send email" });
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});