forked from CSI-KJSCE/appointment_to_examiner
XLSX- css and xlsx-mail
This commit is contained in:
@@ -1,20 +1,90 @@
|
||||
// /routes/email.js
|
||||
// // /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: `Excel File for ${teacher}`,
|
||||
// text: `Attached is the Excel file for ${teacher}.`,
|
||||
// attachments: [
|
||||
// {
|
||||
// filename: fileName,
|
||||
// path: filePath,
|
||||
// contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // MIME type for xlsx
|
||||
// },
|
||||
// ],
|
||||
// };
|
||||
|
||||
// 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;
|
||||
|
||||
|
||||
const express = require("express");
|
||||
const nodemailer = require("nodemailer");
|
||||
const fs = require("fs");
|
||||
const multer = require("multer");
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
const { teacher, csvData, fileName, recipientEmail } = req.body;
|
||||
// Setup multer for handling file uploads
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, "./"); // Save to the current directory (you can customize this)
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
cb(null, file.originalname); // Use the original file name
|
||||
},
|
||||
});
|
||||
|
||||
if (!teacher || !csvData || !fileName || !recipientEmail) {
|
||||
return res.status(400).json({ error: "Missing required fields" });
|
||||
const upload = multer({ storage });
|
||||
|
||||
// Route to handle email sending with file attachment
|
||||
router.post("/", upload.single("file"), async (req, res) => {
|
||||
const { teacher, fileName, recipientEmail } = req.body;
|
||||
|
||||
if (!teacher || !fileName || !recipientEmail || !req.file) {
|
||||
return res.status(400).json({ error: "Missing required fields or file" });
|
||||
}
|
||||
|
||||
// Save the CSV data to a temporary file
|
||||
const filePath = `./${fileName}`;
|
||||
fs.writeFileSync(filePath, csvData);
|
||||
|
||||
// Configure Nodemailer transporter
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: "gmail",
|
||||
@@ -22,21 +92,18 @@ router.post("/", async (req, res) => {
|
||||
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}.`,
|
||||
subject: `Excel File for ${teacher}`,
|
||||
text: `Attached is the Excel file for ${teacher}.`,
|
||||
attachments: [
|
||||
{
|
||||
filename: fileName,
|
||||
path: filePath,
|
||||
path: req.file.path, // Use the uploaded file's path
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -46,7 +113,7 @@ router.post("/", async (req, res) => {
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
// Delete the temporary file after sending the email
|
||||
fs.unlinkSync(filePath);
|
||||
fs.unlinkSync(req.file.path);
|
||||
|
||||
res.status(200).json({ message: "Email sent successfully" });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user