节点:使用传递流到 Nodemailer [英] Node: Using a passthrough stream to Nodemailer

查看:56
本文介绍了节点:使用传递流到 Nodemailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 officegen 生成一个 Word 文档,然后我计划使用 Nodemailer(和 Sendgrid)将其附加到电子邮件中.

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

officegen 输出一个流,但我更愿意将它直接传递给附件,而不是在本地保存 Word 文档然后附加它.

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);
});

推荐答案

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

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
}];

这篇关于节点:使用传递流到 Nodemailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆