节点:使用直通流到Nodemailer

时间:2014-11-07 19:42:37

标签: node.js stream sendgrid nodemailer

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

1 个答案:

答案 0 :(得分:4)

您可以将流对象直接传递给content。 officegen似乎不支持管道,所以你需要一个passthrough流来处理这个

var PassThrough = require('stream').PassThrough;
var docstream = new PassThrough();
docx.generate(docstream);
...
var attachments = [{ // stream as an attachment
    filename: 'out.docx',
    content: docstream
}];