【问题标题】:How to attach a node.js readable stream to a Sendgrid email?如何将 node.js 可读流附加到 Sendgrid 电子邮件?
【发布时间】:2014-07-16 18:29:45
【问题描述】:

我正在使用PDFKit 生成 PDF。我正在使用 Nodejitsu 进行托管,所以我无法将 PDF 保存到文件中,但我可以将它们保存到可读流中。我想将该流附加到 Sendgrid 电子邮件中,如下所示:

sendgrid.send({
    to: email,
    files: [{ filename: 'File.pdf', content: /* what to put here? */ }]
    /* ... */
});

我试过doc.output() 无济于事。

【问题讨论】:

    标签: node.js sendgrid node-pdfkit


    【解决方案1】:

    要将 SendGrid nodejs API 与流文件一起使用,只需将流转换为缓冲区。您可以使用 stream-to-array 将可读流转换为缓冲区。

    var streamToArray = require('stream-to-array');
    
    streamToArray(your_stream, function (err, arr) {
      var buffer = Buffer.concat(arr)
      sendgrid.send({
        to: email,
        files: [{ filename: 'File.pdf' content: buffer }]
      })
    })
    

    【讨论】:

      【解决方案2】:

      尝试通过streamToArray 包发送流并将其编码为base64。 (如果没有base64编码,Sendgrid会抛出no content string的错误)

       // createPdf() module returns the doc stream
       // styling etc etc
       doc.end()      
       return doc // then once doc.end() is called, return the stream
      
       const sgMail = require('@sendgrid/mail');
       const streamToArray = require('stream-to-array');
      
       streamToArray(createPdf(), function (err, arr) { // createPdf() returns the doc made by pdfKit
              var buffer = Buffer.concat(arr).toString('base64')
              msg["subject"] = 'Here is your attachment'
              msg["html"] = body // your html for your email, could use text instead
              msg["attachments"] = [{
                  filename: 'attachment.pdf',
                  content: buffer,
                  type: 'application/pdf',
                  disposition: 'attachment'
              }]
      
              sgMail.send(msg) // sendgrid
            })
      

      【讨论】:

        【解决方案3】:

        我最近自己也在为这个问题苦苦挣扎,我只是想分享一下对我有用的方法:

        //PdfKit
        
        var doc = new PDFDocument({
            size: 'letter'
        });
        doc.text('My awesome text');
        doc.end();
        
        
        //Sendgrid API
        
        var email = new sendgrid.Email();
        
            email.addTo         ('XXX@gmail.com');
            email.setFrom       ('XXX@gmail.com');
            email.setSubject    ('Report');
            email.setText       ('Check out this awesome pdf');
            email.addFile       ({
                    filename: project.name + '.pdf',
                    content: doc,
                    contentType: 'application/pdf'
                });
        
        sendgrid.send(email, function(err, json){
            if(err) {return console.error(err);}
            console.log(json);
        });
        

        关键是让文件附件中的内容成为“doc”

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-21
          • 2020-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多