2

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);
});
ChrisE
  • 35
  • 5

1 Answers1

4

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
}];
Andris
  • 27,649
  • 4
  • 34
  • 38