0% found this document useful (0 votes)
9 views

User - Js CSV

The document shows code for sending emails using Nodemailer and Express. It includes setting up transport and authentication with Gmail, sending emails in a loop, and sending status updates with Server-Sent Events.

Uploaded by

edgesingh8791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

User - Js CSV

The document shows code for sending emails using Nodemailer and Express. It includes setting up transport and authentication with Gmail, sending emails in a loop, and sending status updates with Server-Sent Events.

Uploaded by

edgesingh8791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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: '[email protected]',
// pass: 'uadndlkrqlldzjsd'
// }
// });

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


// let email= emails[i]
// await transporter.sendMail({
// from: '[email protected]',
// 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: '[email protected]',
// pass: 'uadndlkrqlldzjsd'
// }
// });

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


// let email = emails[i];
// await transporter.sendMail({
// from: '[email protected]',
// 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: '[email protected]',
pass: 'uadndlkrqlldzjsd'
}
});

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


let email = emails[i];
try {
await transporter.sendMail({
from: '[email protected]',
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