Google Drive API在电子邮件中发送PDF附件



我有一些node.js代码,使用Google Drive API读取PDF文件Drive .files.get()…(这是一个来自我们Google团队/共享驱动器的文件)。我将返回的流转换为单个base64数据块,如下所示:

// read using drive.files.get() into 'pdfStream'
//...
let pdf64 = '';
pdfSream.on('readable', function () { 
var chunk = pdfStream.read();
if (chunk != null) {
var chunk64 = chunk.toString('base64');
pdf64 += chunk64;
}
});
pdfStream.on('end', function () {
// Do SendGrid email with pdf64 as attachment
});

注意,最终目标是发送带有PDF的电子邮件。我没有包含太多的代码,因为这一切都很好-只要电子邮件收件人在我们公司的域上。对于外部电子邮件收件人,PDF附件是不可查看的,无法下载-至少这是目前的情况。

我不认为访问限制和权限会停留在使用drive.files.get()直接读取的数据上。这是一件事吗?我怀疑是SendGrid,除非我们在代码的其他区域发送附件没有问题。

的想法吗?感谢!~鲍勃

我能够修复它。权限问题是一个转移注意力的问题——pdf附件被破坏了,我们公司的电子邮件系统比其他公司(比如Gmail)对错误更宽容。我重构了上面的代码,首先创建一个完整的数据数组,然后将该数组转换为base64:

// read using drive.files.get() into 'pdfStream'
//...
let pdfChunks = [];
// Read through stream chunks and concat them together
pdfStream.on('readable', function () {
var chunk = pdfStream.read();
if (chunk != null) {
pdfChunks.push(chunk);
}
});
// On the end of the stream, convert to base64 and email the Pdf
pdfStream.once('end', function () {
let pdfBin = Buffer.concat(pdfChunks);
let pdf64 = pdfBin.toString('base64');
//...
// Do SendGrid email with pdf64 as attachment
});

干杯!~鲍勃

最新更新