// /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;