使用Mailgun,pdfkit在Node.js中通过mailgunl动态发送的邮件文件中接收的附件pdf出错

问题描述

在我的nodejs应用程序中,我正在尝试发送动态生成的pdf作为电子邮件附件。我可以在系统上生成pdf文件(在给定路径下,也可以打开该文件)。我还可以将文本消息和附件作为invoice.pdf发送给接收者。所以到现在为止一切都很好。

但是,收到的invoice.pdf发生错误。尝试打开文件时,收到的消息格式无效。

我认为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();

  doc.pipe(fs.createWriteStream("invoice1.pdf"));
  doc.pipe(res);

  var api_key = process.env.api_key;
  var domain = process.env.domain;

  var mailgun = require("mailgun-js")({ apiKey: api_key,domain: domain });

  doc
    .fontSize(25)
    .text("Hello where is my attachment! Error attachment",100,100);

  doc.end();
  var file = fs.readFileSync("invoice1.pdf");

  var attch = new mailgun.Attachment({ data: file,filename: "invoice1.pdf" });

  var data = {
    from: process.env.from,to: process.env.to,subject: "Hello",text: "Testing some Mailgun awesomeness!",attachment: attch,};

  mailgun.messages().send(data,function (error,body) {
    console.log(body);
  });
};

module.exports = getPdf;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

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