我正在使用 pdfkit 制作一个 pdf 文件,并将其作为附件发送到使用 nodemailer 的电子邮件,但它发送 0 字节 output.pdf。
我猜问题出在函数执行顺序上——它在创建 pdf 文件之前开始发送电子邮件。我只是不明白如何解决它...
app.post("/addressofpost", function(req, res){
var abc = req.body.entpost;
var subj = "pdf text"
"use strict"
const doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
doc.font('PalatinoBold.ttf')
.fontSize(25)
.text(subj, 100, 100);
doc.end();
async function main(){
let account = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp settings",
port: 465,
secure: true,
auth: {
user: "mailuser",
pass: "mailpass"
}
});
let mailOptions = {
from: '"Sender" <sender@gmail.com',
to: '"Receiver" <receiver@gmail.com>',
subject: "Subject",
text: "email text",
html: "HTML text"
};
let mailOptionsPrint = {
attachments: [
{
filename: 'output.pdf'
}],
from: '"Sergei Dudin" <dudinsergey@mail.ru>',
to: '"Принтер" <info@moypohod.ru>',
subject: "Subject",
text: "email text",
html: "HTML text"
};
let info = await transporter.sendMail(mailOptions)
let infoPrint = await transporter.sendMail(mailOptionsPrint)
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
main().catch(console.error);
console.log(abc);
res.send('done');
});
感谢您的任何帮助!