生成动态pdf并使用nodemailer,pdfkit和mailgun将其作为附件发送到nodejs中的电子邮件中

问题描述

在我的Nodejs应用程序中,我正在使用pdfkit生成动态PDF文件。我想使用mailgun将文件作为附件发送到收件人邮件。我能够将文本消息发送到接收器,也能够在系统中生成PDF并将其显示在浏览器中。但是,附件文件在收件人的末端丢失。怎么了?

 const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
const PDFDocument = require("pdfkit");

dotenv.config({ path: "./config/config.env" });

const getPdf = (req,res,next) =>{

  const doc = new PDFDocument();
  const invoiceName = "invoice.pdf";
  const invoicePath = path.join("data",invoiceName);

  var filepath = doc.pipe(fs.createWriteStream(invoicePath));
  doc.pipe(res);
  doc.fontSize(25).text("Hello where is my attachment!",100,100);

    doc.end();
  
    var api_key = process.env.api_key;
    var domain = process.env.domain;
    var mailgun = require("mailgun-js")({ apiKey: api_key,domain: domain });
  
   
  
    var data = {
      from: process.env.from,to: process.env.to,subject: "Hello",text: "HELLO WORLD",attachments: filepath
    };
  
    mailgun.messages().send(data,function (error,body) {
      if (error) {
        console.log(error);
      } else {
        res.render("confirmation.ejs",{
          pageTitle: "Message Sent",messageSent: "Message has been sent succesfully",});
        
      }
    });
    
  
};

module.exports = getPdf;

解决方法

根据documentation,配置可以包含一个附件参数,但是,在您的代码中,您拼写了附件(而不是附件)一词。

配置应为


    var data = {
      from: process.env.from,to: process.env.to,subject: "Hello",text: "HELLO WORLD",attachment: filepath
    };

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...