我正在使用officegen 生成一个Word 文档,然后我计划使用Nodemailer(和Sendgrid)将其附加到电子邮件中。
officegen 输出一个流,但我更愿意将其直接传递到附件,而不是在本地保存 Word 文档然后附加它。
// Generates output file
docx.generate(fs.createWriteStream ('out.docx'));
var client = nodemailer.createTransport(sgTransport(options));
var email = {
from: 'email@here',
to: user.email,
subject: 'Test Email send',
text: 'Hi!\n\n' +
'This is a test email.'
attachments: [{ // stream as an attachment
filename: 'out.docx',
content: 'out.docx' // Ideally, I'd like this to be a stream through docx.generate()
}]
};
client.sendMail(email, function(err, info) {
if (err) {
console.log(err);
return res.send(400);
}
return res.send(200);
});
最佳答案
您可以将流对象直接传递给内容
。 officegen 似乎不支持管道,因此您需要一个直通流来处理此问题
var PassThrough = require('stream').PassThrough;
var docstream = new PassThrough();
docx.generate(docstream);
...
var attachments = [{ // stream as an attachment
filename: 'out.docx',
content: docstream
}];
关于node.js - Node : Using a passthrough stream to Nodemailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808871/