尝试通过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
})