繁体   English   中英

如何使用node.js中的nodemailer修复收到的0字节邮件附件

[英]How to fix mail attachment received with 0 bytes using nodemailer in node.js

我正在使用节点-10.14.1和nodemailer-4.7.0

我的问题是,当我从excel附件中的数据库中发送单行数据时,我能够接收带有附件的邮件,但是当我从excel中的数据库中的附件中作为附件发送数据库中的多行数据时,我却收到了0字节的附件,并且仍然显示附件中的下载。 我的文件中也有图像。 在哪里我可以成功生成整个Excel文件,但是在附加文件时却失败了...。我也尝试了sendgrid,但遇到了同样的问题。

有人可以帮我解决我的代码,我要去哪里了.....

我的代码是-

exports.getExcelFile = (req, res, next) => {
   const excelFile = {};
   Order.findById(req.params.id).then(order => {
      excelFile.excelOffFile = "Off_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelOffFilePath = path.join("data", "excel", excelFile.excelOffFile);
      excelFile.excelFactFile = "Fact_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelFactFilePath = path.join("data", "excel", excelFile.excelFactFile);
      excelFile.excelBrFile = "Br_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelBrFilePath = path.join("data", "excel", excelFile.excelBrFile);
      excelFile.excelSuFile = "Su_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelSuFilePath = path.join("data", "excel", excelFile.excelSuFile);

      let workbookFinal = createExcelFile(order, "C", excelFile.excelOffFilePath);

      createExcelFile(order, "F", excelFile.excelFactFilePath);
      createExcelFile(order, "B", excelFile.excelBrFilePath);
      createExcelFile(order, "S", excelFile.excelSuFilePath);

      res.setHeader("Content-Type", "application/vnd.ms-excel");
      res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
      workbookFinal.xlsx.write(res).then(() => {
         res.end();
      });

      //-------- Sending Mail --------
      transporter.sendMail({
         from: defaultMailId,
         to: config.get("mail.defaultAdd"),
         subject: "Order with excel attachment send through software ",
         html: `<h1>
         The mail has been sent as trial through program for testing excel file attachement.
         </h1>
         <h3>
            file 1- ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelOffFile })}</br>
            file 2- ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelFactFile })}</br>
            file 3- ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelBrFile })}</br>
            file 4- ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelSuFile })}</br>
         </h3>
         `,
         attachments: [
            { 
               content: fs.readFileSync(excelFile.excelOffFilePath, { encoding: "base64" }),
               filename: excelFile.excelOffFile,
               type: "application/vnd.ms-excel"
            },
            {
               content: fs.readFileSync(excelFile.excelFactFilePath, { encoding: "base64" }),
               filename: excelFile.excelFactFile,
               type: "application/vnd.ms-excel"
            },
            {
               content: fs.readFileSync(excelFile.excelBrFilePath, { encoding: "base64" }),
               filename: excelFile.excelBrFile,
               type: "application/vnd.ms-excel"
            },
            {
               path: excelFile.excelSuFilePath,
            }
         ]
      }, (err, info) => {
         if (err) { return console.log(err); }
         console.log("message Send");
      });
   }).catch(err => console.log(err));
};

我想到了。 发送邮件之前未成功创建excel文件。 因此,将邮件代码移出了新功能并正常工作了...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM