Node: Using a passthrough stream to Nodemailer

╄→гoц情女王★ 提交于 2019-12-24 02:17:16

问题


I'm generating a Word document using officegen that I then plan to attach to an email using Nodemailer (and Sendgrid).

officegen outputs a stream, but I'd prefer to pass that straight through to the attachment rather than saving the Word document locally and then attaching it.

// 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:


You can pass a stream object directly to content. officegen doesn't seem to support pipeing, so you need a passthrough stream to handle this

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


来源:https://stackoverflow.com/questions/26808871/node-using-a-passthrough-stream-to-nodemailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!