Saturday 8 April 2017

I am trying to send mail using node mailer. But it is getting timedout . I need to send the mail for 6000 employees. I cannot send it. Please Help me

router.post('/sendmail', function(req, res) { // sending mail 

      /*
       *
       * Task : Run Server and fetch multiple emails from DB to send reminder
       * Invoke all the email task at once and update DB once the email is sent 
       */

      var listofemails = [];
      var listofempids = [];
      var listofempnames = [];
      var empname;
      var empid;
      var j = -1;
      var y = moment().format('YYYY');
      var m = moment().subtract(1, 'months').format('MMMM');

      var dbs = monk('localhost:27017/payroll');
      var collection = dbs.get('employee');
      collection.find({
            $and: [{
              "month": m
            }, {
              "year": y
            }]
          }, function(e, docs) {

            if (e) {
              console.log(e);
            } else {

              //   console.log(docs);    


            }
            for (var i = 0; i < docs.length; i++) {
              listofemails.push(docs[i].emailid);
              listofempids.push(docs[i].empid);
              listofempnames.push(docs[i].employeename);

              // console.log(emaillist);


            }

            // Will store email sent successfully.

            //var listofemails = [list];

            var success_email = [];
            // Will store email whose sending is failed. 
            var failure_email = [];

            var transporter;

            /* Loading modules done. */


            function massMailer() {
              var self = this;
              transporter = nodemailer.createTransport({
                host: 'smtp.gmail.com',
                port: 587,
                auth: {
                  user: 'myhub@aditya.ac.in',
                  pass: 'Thub@123'
                },
                tls: {
                  rejectUnauthorized: false
                },
                debug: true
              });
              // Fetch all the emails from database and push it in listofemails
              self.invokeOperation();
            };

            /* Invoking email sending operation at once */

            massMailer.prototype.invokeOperation = function() {
                var self = this;
                /*async.each(listofempnames,self.SendEname,function(){

                });
                 async.each(listofempids,self.SendEmpid,function(){

                });*/
                async.each(listofemails, self.SendEmail, function() {
                  console.log(success_email);
                  console.log(failure_email);
                });


              }
              /*massMailer.prototype.SendEname = function(Ename,callback) {

               async.waterfall([
                  function(callback) { 

                  },
                      ],function(){
                      //When everything is done return back to caller.
                      callback();
              });

              }
              massMailer.prototype.SendEmpid = function(Empids,callback) {
              empid=Empids;
               async.waterfall([
                  function(callback) { 

                  },
                      ],function(){
                      //When everything is done return back to caller.
                      callback();
              });

              }*/

            /* 
              * This function will be called by multiple instance.
              * Each instance will contain one email ID
              * After successfull email operation, it will be pushed in failed or 
                success 
                 array.
             */

            massMailer.prototype.SendEmail = function(Email, callback) {
              console.log("Sending email to " + Email);
              var self = this;
              self.status = false;

              var months = ["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"
              ];

              var monthInt = new Date().getMonth();
              var year = new Date().getFullYear();
              var monthString = months[monthInt - 1];

              var filename = monthString + '-' + year + '-' + listofempids[j + 1] + '.pdf';
              var a = monthString + ' ' + year;
              //  console.log(filename,a);

              var msg = "<!DOCTYPE html><html><head><meta name='viewport' 
                            content='width=device-width'><meta http-equiv='Content-Type' 
                            content='text/html; charset=UTF-8'><title>Payslip Email</title><style 
                            type='text/css'>@media only screen and (max-width: 800px) 
                            {table[class=body] h1 {font-size: 28px !important;table[class=body] 
                            p,table[class=body] ul,table[class=body] ol,table[class=body] 
                            td,table[class=body] span,table[class=body] a {font-size: 16px !important; 
                            table[class=body] .wrapper,table[class=body] .article {padding: 10px 
                            !important; }table[class=body] .content {padding: 0 !important; 
                            }table[class=body] .container {padding: 0 !important;width: 100% 
                            !important; }table[class=body] .main {border-left-width: 0 
                            !important;border-radius: 0 !important;border-right-width: 0 !important; 
                            }table[class=body] .btn table {width: 100% !important; }table[class=body] 
                            .btn a {width: 100% !important; }table[class=body] .img-responsive 
                            {height: auto !important;max-width: 100% !important;width: auto 
                            !important; }}@media all {.ExternalClass {width: 100%; 
                            }.ExternalClass,.ExternalClass p,.ExternalClass span,.ExternalClass 
                            font,.ExternalClass td,.ExternalClass div {line-height: 100%; }.apple-link 
                            a {color: inherit !important;font-family: inherit !important;font-size: 
                            inherit !important;font-weight: inherit !important;line-height: inherit 
                            !important;text-decoration: none !important; }.btn-primary table td:hover 
                            {background-color: #34495e !important; }.btn-primary a:hover {background-
                            color: #34495e !important;border-color: #34495e !important; } }.style1 
                            {font-size: 9px}</style></head><body class='' style='background-
                             color:#f6f6f6;font-family:sans-serif;-webkit-font-
                             smoothing:antialiased;font-size:14px;line-height:1.4;margin:0;padding:0;-
                             ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;'><table 
                             border='0' cellpadding='0' cellspacing='0' class='body' style='border-
                             collapse:separate;mso-table-lspace:0pt;mso-table-rspace:0pt;background-
                              color:#f6f6f6;width:100%;'><tr><td style='font-family:sans-serif;font-
                             size:14px;vertical-align:top;'>&nbsp;</td><td class='container' 
                             style='font-family:sans-serif;font-size:14px;vertical-
                             align:top;display:block;max-width:580px;padding:10px;width:580px;Margin:0 
                             auto !important;'><div class='content' style='box-sizing:border-
                             box;display:block;Margin:0 auto;max-width:580px;padding:10px;'><!-- START 
                             CENTERED WHITE CONTAINER --><span class='preheader' 
                             style='color:transparent;display:none;height:0;max-height:0;max-
                             width:0;opacity:0;overflow:hidden;mso-
                             hide:all;visibility:hidden;width:0;'></span><table class='main' 
                             style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
                             rspace:0pt;background:#fff;border-radius:3px;width:100%;'><tr><td 
                             class='wrapper' style='font-family:sans-serif;font-size:14px;vertical-
                             align:top;box-sizing:border-box;padding:20px;'><table border='0' 
                             cellpadding='0' cellspacing='0' style='border-collapse:separate;mso-
                             table-lspace:0pt;mso-table-rspace:0pt;width:100%;'><tr><td style='font-
                             family:sans-serif;font-size:14px;vertical-align:top;'><p align='right' 
                             style='font-family:sans-serif;font-size:14px;font-
                             weight:normal;margin:0;Margin-bottom:15px;'><img 
                             src='http://ecourses.aec.edu.in/thub/images/myhub_aditya.png' width='84' 
                             height='50' alt='AdityaLogo'><p style='font-family:sans-serif;font-
                             size:14px;font-weight:normal;margin:0;Margin-bottom:15px;'>Hi"+" 
                             "+listofempnames[j+1]+",<p style='font-family:calibri;font-
                             color:#295890;font-size:14px;font-weight:normal;margin:0;Margin-
                             bottom:15px;'>Please find your payslip for the month of"+" 
                             "+monthString+" "+year+" "+"attached with the mail.<p>Regards,<br><img 
                             src='http://ecourses.aec.edu.in/thub/images/myhublogo.png' width='84' 
                             height='50' alt='MyHubLogo'><br>Payroll processing system</p><br><p 
                             style='font-family:sans-serif;font-size:10px;font-
                             weight:normal;margin:0;Margin-bottom:15px;'> Your PDF Payslip is 
                             delivered to your inbox, and you will be able to open the PDF attachment, 
                             only if you have Adobe reader version 7.0.8 and above. If Adobe Reader is 
                             not installed / upgraded in your PC. <a 
                             href='https://acrobat.adobe.com/in/en/acrobat/pdf-reader.html'>click 
                             here</a>.</p><div align='justify'><br></div><p style='font-family:sans-
                             serif;font-size:10px;font-weight:normal;margin:0;Margin-bottom:15px;'>The 
                             information contained in this electronic message and any attachments to 
                             this message are intended for the exclusive use of the addressee(s) and 
                             may contain proprietary, confidential or privileged information. If you 
                             are not the intended recipient, you should not disseminate, distribute or 
                             copy this e-mail. Please notify the sender immediately and destroy all 
                             copies of this message and any attachments. WARNING: Computer viruses can 
                             be transmitted via email. The recipient should check this email and any 
                             attachments for the presence of viruses. The management accepts no 
                             liability for any damage caused by any virus transmitted by this email.
                             </p><table border='0' cellpadding='0' cellspacing='0' class='btn btn-
                             primary' style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
                             rspace:0pt;box-sizing:border-box;width:100%;'><tbody><tr><td align='left' 
                              style='font-family:sans-serif;font-size:14px;vertical-align:top;padding-
                              bottom:15px;'><center><table border='0' cellpadding='0' cellspacing='0' 
                              style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
                              rspace:0pt;width:100%;width:auto;'><tbody><tr><td style='font-
                              family:sans-serif;font-size:14px;vertical-align:top;background-
                              color:#ffffff;border-radius:5px;text-align:center;background-color:;'> 
                              </td></tr></tbody></table></center></td></tr></tbody></table><p 
                              style='font-family:sans-serif;font-size:14px;font-
                              weight:normal;margin:0;Margin-bottom:15px;'></p><p style='font-
                              family:sans-serif;font-size:11px;font-weight:normal;margin:0;Margin-
                              bottom:15px;text-align:center;'>Have a Good Day!.</p></td></tr></table>
                              </td></tr></table><!-- START FOOTER --><div class='footer' 
                              style='clear:both;padding-top:10px;text-align:center;width:100%;'><table 
                              border='0' cellpadding='0' cellspacing='0' style='border-
                              collapse:separate;mso-table-lspace:0pt;mso-table-
                              rspace:0pt;width:100%;'><tr><td class='content-block' style='font-
                              family:sans-serif;font-size:14px;vertical-align:top;color:#999999;font-
                              size:12px;text-align:center;'><span class='apple-link' 
                              style='color:#999999;font-size:12px;text-align:center;'>Aditya 
                              Educational Institutions,Surampalem,533437</span><br>This email is 
                              system generated, please do not respond to this email.</td></tr><tr><td 
                              class='content-block powered-by' style='font-family:sans-serif;font-
                              size:14px;vertical-align:top;color:#999999;font-size:12px;text-
                              align:center;'>Powered by <a href='http://thub.ac.edu.in' 
                              style='color:#3498db;text-decoration:underline;color:#999999;font-
                              size:12px;text-align:center;text-decoration:none;'>T-HUB team</a>.</td>
                              </tr></table></div><!-- END FOOTER --><!-- END CENTERED WHITE CONTAINER 
                              --></div></td><td style='font-family:sans-serif;font-size:14px;vertical-
                              align:top;'>&nbsp;</td></tr></table></body></html>";
                      // waterfall will go one after another
                      // So first email will be sent
                      // Callback will jump us to next function
                      // in that we will update DB
                      // Once done that instance is done.
                      // Once every instance is done final callback will be called.
                      j++;
                      async.waterfall([
                          function(callback) {
                            var mailOptions = {
                              from: '"myHUB " <myhub@aditya.ac.in>',
                              to: Email,
                              subject: 'Payslip for the month of' + ' ' + '  ' + monthString + '  ' + ' 
                              '+year, 
                              text: 'Payslip', // plain text body
                              html: msg, // html body

                              attachments: [
                                //{data:msg, alternative:true},
                                {
                                  path: "C:/users/admin/downloads/" + filename,
                                  type: "application/pdf",
                                  name: filename
                                }
                              ]

                            };
                            transporter.sendMail(mailOptions, function(error, info) {
                              if (error) {
                                console.log(error)
                                failure_email.push(Email);
                              } else {
                                self.status = true;
                                success_email.push(Email);
                              }
                              callback(null, self.status, Email);
                            });
                          },
                          function(statusCode, Email, callback) {
                            console.log("Mail sent for " + Email + "With " + statusCode);

                            callback();
                          }

                        ], //

                        function(j) {
                          //When everything is done return back to caller.
                          if (j == 6) {
                            transporter.close();
                          } else {
                            callback();
                          }
                        });
                      // res.json("hai");

                    }

                    new massMailer(); //lets begin
                  });
              });



via Raakesh Kumar

No comments:

Post a Comment