Node-mailer: Problem with rendering node mail images in the body; bug ID: 146469

25 Views Asked by At

There is an intermediate problem with some mails having a partially rendered image that comes in the mail body using nodemailer.

htmlForMailBody += '<div>' + txt.split("$reportname").join(reportName).split("$sddate").join(reportStartDate).split("$eddate").join(reportEndDate).split("$genDateTime").join(getDateForReports()) + '</div><br><div> URL = <a href=' + genURLFile + '>' + genURLFile + '</a></div><br>';
         if (chartImagePathArr != undefined && chartImagePathArr.length > 0) {
             htmlForMailBody += '<div>';
             for (let i = 0; i < chartImagePathArr.length; i++) {
                 let chartImgPathObj = chartImagePathArr[i]['imgPath'];
                 let obj = {
                     path: ipPort + chartImgPathObj,
                     cid: 'chartImage_' + i
                 }
                 attachmentsArray.push(obj);
                 chartImagePathArr[i].rptSetName = undoReplaceName(chartImagePathArr[i].rptSetName) + "...";

                 if (chartImagePathArr.length > 3) {
                     if (chartImagePathArr.length % 2 == 0)
                         htmlForMailBody += '<div style=\'padding:3px;width:50%;box-sizing:border-box;float:left\'><label style=\'background:#081f3c;color:#fff;font-size:12px;padding:2px;text-overflow:ellipsis;white-space:nowrap;display:inline-block;overflow:hidden;width:100%\'>' + chartImagePathArr[i].rptSetName + '</label><img src=cid:chartImage_' + i + ' ></div>';
                     else {
                         if (i == chartImagePathArr.length - 1)
                             htmlForMailBody += '<div style=\'padding:3px;box-sizing:border-box\'><label style=\'background:#081f3c;color:#fff;font-size:12px;padding:2px;text-overflow:ellipsis;white-space:nowrap;display:inline-block;overflow:hidden;width:100%\'>' + chartImagePathArr[i].rptSetName + '</label><img src=cid:chartImage_' + i + ' ></div>';
                         else
                             htmlForMailBody += '<div style=\'padding:3px;width:50%;box-sizing:border-box;float:left\'><label style=\'background:#081f3c;color:#fff;font-size:12px;padding:2px;text-overflow:ellipsis;white-space:nowrap;display:inline-block;overflow:hidden;width:100%\'>' + chartImagePathArr[i].rptSetName + '</label><img src=cid:chartImage_' + i + '></div>';
                     }

                 } else
                     htmlForMailBody += '<div style=\'padding:3px;box-sizing:border-box\'><label style=\'background:#081f3c;color:#fff;font-size:12px;padding:2px;text-overflow:ellipsis;white-space:nowrap;display:inline-block;overflow:hidden;width:100%\'>' + chartImagePathArr[i].rptSetName + '</label><img src=cid:chartImage_' + i + '></div>';

             }
             htmlForMailBody += '</div>';
         }

         if (attachmentsArray.length > 0) {
             schedulerLog("Creating Mail Option with attachments", 1);
             mailOptions = {
                 html: htmlForMailBody,
                 from: global.mailConfInfo['user'], // sender address
                 to: mailObj.to, // list of receivers
                 subject: sub.split("$reportname").join(reportName).split("$sddate").join(reportStartDate).split("$eddate").join(reportEndDate).split("$genDateTime").join(getDateForReports()), // Subject line
                 attachments: attachmentsArray
             };
         } else {
             schedulerLog("Creating Mail Option without attachments", 1);
             mailOptions = {
                 html: htmlForMailBody,
                 from: global.mailConfInfo['user'], // sender address
                 to: mailObj.to, // list of receivers
                 subject: sub.split("$reportname").join(reportName).split("$sddate").join(reportStartDate).split("$eddate").join(reportEndDate).split("$genDateTime").join(getDateForReports()) // Subject line
             };
         }

         let msStartDate = new Date();
         console.error("send mail StartDate time for checing delay", msStartDate);

         msStartDate = new Date();
         console.error("setTimeout checing delay time :", msStartDate);
         transporter.sendMail(mailOptions, function (error, info) {
             try {
                 msStartDate = new Date();
                 console.error("setTimeout transporter try block on send mail delay time :", msStartDate);
                 if (typeOfReport == 2) {
                     const fs = require('fs');
                     fs.unlinkSync(filename);  //Removing tar file of HTML Report
                 }
             } catch (err) {
                 console.error("setTimeout transporter catch block on send mail delay time :", msStartDate);
                 schedulerLog("Error on deleting tar file of html report with error Info == " + err, 1);
             }
             if (error) {
                 return schedulerLog("Error in sending mail, error info is = " + JSON.stringify(error), 2);
             }
             schedulerLog("Mail is sent with error Info == " + JSON.stringify(error), 2);
             schedulerLog("Mail is sent with success Info == " + JSON.stringify(info), 1);
         });

Note : This problem won't keep coming up. Sometimes it does work.

we want some solution for this and want to know why this issue is comming in mid of mail scheduling.

0

There are 0 best solutions below