You are on page 1of 3

user.

js csv
import express from "express";
import nodemailer from 'nodemailer';

const app = express();


app.use(express.json());
const router = express.Router();

// router.post('/Dashboard', async (req,res)=>{


// const { emails,textmsg,subject } = req.body;
// console.log(emails,textmsg,subject)
// try {
// const transporter = nodemailer.createTransport({
// // configure your email service
// service: 'Gmail',
// auth: {
// user: 'Hariomsingh8791@gmail.com',
// pass: 'uadndlkrqlldzjsd'
// }
// });

// for (let i=0; i<emails.length; i++) {


// let email= emails[i]
// await transporter.sendMail({
// from: 'Hariomsingh8791@gmail.com',
// to: email,
// subject: `${subject}`,
// text: `${textmsg}`

// });
// console.log(email,'Emails sent successfully');
// }
// console.log('Emails sent successfully');

// return res.json({ status: true, message: 'Emails final successfully'});


// } catch (error) {
// console.error('Error sending emails:', error);
// return res.json({status:false, error: 'Failed to send emails' });
// }
// });
//above code is working one

// router.get('/DashboardUpdates', (req, res) => {


// console.log("updates call")
// res.setHeader('Content-Type', 'text/event-stream');
// res.setHeader('Cache-Control', 'no-cache');
// res.setHeader('Connection', 'keep-alive');

// const { emails, textmsg, subject } = req.query;


// console.log(emails[0],emails[1],emails[2])
// const sendEmails = async () => {
// try {
// const transporter = nodemailer.createTransport({
// // configure your email service
// service: 'Gmail',
// auth: {
// user: 'Hariomsingh8791@gmail.com',
// pass: 'uadndlkrqlldzjsd'
// }
// });

// for (let i = 1; i < 3; i++) {


// let email = emails[i];
// await transporter.sendMail({
// from: 'Hariomsingh8791@gmail.com',
// to: email,
// subject: `${subject}`,
// text: `${textmsg}`
// });
// console.log(email, 'Eail sent successfully');
// res.write(`data: Eail sent to ${email}\n\n`);
// }

// console.log('All eails sent successfully');


// res.write(`data: All eails sent successfully\n\n`);
// res.end();
// } catch (error) {
// console.error('Error sending emails:', error);
// res.write(`data: Error sending emails: ${error.message}\n\n`);
// res.end();
// }
// };

// sendEmails();
// });

router.post('/Dashboard', async (req, res) => {


const { emails, textmsg, subject } = req.body;
console.log(emails, textmsg, subject);

// Set up SSE headers


res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');

try {
const transporter = nodemailer.createTransport({
// configure your email service
service: 'Gmail',
auth: {
user: 'Hariomsingh8791@gmail.com',
pass: 'uadndlkrqlldzjsd'
}
});

for (let i = 0; i < emails.length; i++) {


let email = emails[i];
try {
await transporter.sendMail({
from: 'Hariomsingh8791@gmail.com',
to: email,
subject: `${subject}`,
text: `${textmsg}`
});
console.log(email, 'Email sent successfully');
// Send SSE message to client
res.write(`data: Email sent to ${email}\n\n`);
res.flush();
} catch (error) {
console.error('Error sending email:', error);
// Send SSE message to client in case of error
res.write(`data: Error sending email to ${email}\n\n`);
res.flush();
}
}

console.log('All emails sent successfully');


// Send SSE message to client indicating completion
res.write('data: All emails sent successfully\n\n');

res.end();
} catch (error) {
console.error('Error:', error);
// Send SSE message to client in case of error
res.write('data: Error sending emails\n\n');
res.end();
}
});
export { router as UserRouter };

You might also like