gpt4 book ai didi

node.js - Node : Using a passthrough stream to Nodemailer

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:13 25 4
gpt4 key购买 nike

我正在使用 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);
});

最佳答案

您可以将流对象直接传递给content。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com