【发布时间】:2020-09-20 13:01:51
【问题描述】:
在我的 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;
【问题讨论】:
标签: node.js email-attachments nodemailer mailgun pdfkit