Thursday, 18 May 2017

How do I send bulk mails asynchronously in loop in node js?

I want to send multiple mails to users using asynchronously.

  • user.js File

    var emailUtil = require("./emailUtil.js"); var userdata[100];

    // Suppose in userdata there are multiple data, let it be 100

    for(let i=0;i let emailBody = 'body of the mail', emailSubject = 'SGT Schedule'; emailUtil.sendEmail(emailBody, '' , emailSubject, '' , '' , user_data.email, 'Your Name', ''); }

In the other file where I write my email sending function

  • emailUtil.js

    // Email Util File

    var nodemailer = require('nodemailer');
    
    // Not the movie transporter!
      var transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
              user: 'email id', // Your email id
              pass: '*******' // Your password
          }
      });
    
    
    
    
        module.exports =
    
    {
    
       sendEmail: function sendMail(varhtml,vartext, 
            varsubject,varfrom, varfrom_name,varto, varto_name, 
          reply_to_email ) {
    
    
       //setup e-mail data with unicode symbols
       var mailOptions = {
    
           to: [varto], // list of receivers
           subject: varsubject, // Subject line
           text: vartext, // plaintext body
           html: varhtml // html body
       };
    
    
       console.log(mailOptions);
    
       // send mail with defined transport object
        transporter.sendMail(mailOptions, function (error, info) {
           if (error) {
               return console.log(error);
           }else{
             return console.log(info);
           }
       });
    
    

    } }

I want to send the response immediately. I don't need to wait for the emails So for doing that I am thinking about send mails using loop but asynchronously. Hope I am able to explain my question properly. Please let me know if there is any concern



via VIKAS KOHLI

No comments:

Post a Comment