首页 > 解决方案 > Azure 函数 Sendgrid 附件损坏

问题描述

当包含指定 PDF 的 blob 到达我们的 blob 存储库时,我目前正在使用 Azure Functions 和带有 Sendgrid 的 node.js 向用户发送电子邮件。这很好,但是我现在正在尝试将 PDF 本身附加到电子邮件中。每次我尝试此操作时,PDF 都已附加到电子邮件但无法打开(说它已损坏)。这本质上是这个问题(Azure Function (JS) using SendGrid with attachment),但我不能评论直接询问,因为我是新用户。

但我不明白如何设置附件的内容。

这是我的代码:

var util = require('util');

module.exports = function (context, myBlob) {
    var message = {
       "personalizations": [ { "to": [ { "email": "myEmail@myDomain.org" } ] } ],
        from: { email: "YouveGotMail@AlertSystem.org" },        
        subject: util.format('%s', context.bindingData.name),
        content: [{
            type: 'text/plain',
            value: util.format("New Mail #%s", context.bindingData.name)
        }],
        attachments: [
            {
            content: context.bindings.myBlob,
            filename: util.format('%s.pdf', context.bindingData.name),
            type: 'application/pdf',
            disposition: 'attachment',
            },
        ]

    };
context.done(null, message);
};

我在内容变量中尝试过的其他输入:myBlob,新缓冲区(context.bindings.myBlob,“base64”),“base64”。任何帮助将不胜感激,提前感谢您的时间。

标签: node.jsazureazure-functionssendgrid

解决方案


要使用 base64 编码,您需要先将输入 blob 数据类型更改为二进制。

添加"dataType": "binary"你的function.jsonblob触发器绑定。

然后将您的 PDF 编码为 base64 字符串,

content: new Buffer(myBlob).toString('base64')

推荐阅读