mailing-part 50%

This commit is contained in:
amNobodyyy
2025-01-23 22:42:41 +05:30
parent d96d27154b
commit b39cb67b0f
4 changed files with 154 additions and 193 deletions

View File

@@ -0,0 +1,58 @@
// /routes/email.js
const express = require("express");
const nodemailer = require("nodemailer");
const fs = require("fs");
const router = express.Router();
router.post("/", async (req, res) => {
const { teacher, csvData, fileName, recipientEmail } = req.body;
if (!teacher || !csvData || !fileName || !recipientEmail) {
return res.status(400).json({ error: "Missing required fields" });
}
// Save the CSV data to a temporary file
const filePath = `./${fileName}`;
fs.writeFileSync(filePath, csvData);
// Configure Nodemailer transporter
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "swdc.ate@gmail.com", // Replace with your email
pass: "umlc hbkr dpga iywd", // Replace with your app-specific password or token
},
// tls: {
// rejectUnauthorized: false, // Disable SSL verification
// }
});
// Email options
const mailOptions = {
from: "swdc.ate@gmail.com", // Replace with your email
to: recipientEmail,
subject: `CSV File for ${teacher}`,
text: `Attached is the CSV file for ${teacher}.`,
attachments: [
{
filename: fileName,
path: filePath,
},
],
};
try {
// Send email
await transporter.sendMail(mailOptions);
// Delete the temporary file after sending the email
fs.unlinkSync(filePath);
res.status(200).json({ message: "Email sent successfully" });
} catch (error) {
console.error("Error sending email:", error);
res.status(500).json({ error: "Failed to send email" });
}
});
module.exports = router;

View File

@@ -15,6 +15,7 @@ const facultyRoutes = require("./routes/facultyRoutes");
const appointmentRoutes = require("./routes/appointmentRoutes");
const optionsRoutes = require("./routes/optionsRoutes");
const consolidatedRoutes = require("./routes/consolidatedRoutes");
const emailRoutes = require("./routes/emailRoutes");
const Course = require("./models/Course");
// MongoDB Connection
@@ -56,6 +57,7 @@ app.use("/api/faculty", facultyRoutes);
app.use("/api/appointments", appointmentRoutes);
app.use("/api/options", optionsRoutes);
app.use("/api/data", consolidatedRoutes); // Moved after `app` initialization
app.use("/api/send-email", emailRoutes);
// Google OAuth Routes
app.get(