node.js - 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”。任何帮助将不胜感激,提前感谢您的时间。
解决方案
要使用 base64 编码,您需要先将输入 blob 数据类型更改为二进制。
添加"dataType": "binary"
你的function.json
blob触发器绑定。
然后将您的 PDF 编码为 base64 字符串,
content: new Buffer(myBlob).toString('base64')
推荐阅读
- javascript - PowerShell :: 点击 href="javascript:;" 并下载文件
- apache-kafka - Kafka 领导者与 Kafka 分区领导者选举
- java - 尝试在 apache camel 中调用 Rest DSL 时,邮递员出现 404 错误
- python - 如何通过按键事件 PyQt5 将 textEdit 小部件连接到函数
- weka - LibSVM 使用 Weka 命令行
- typescript - Typescript NestJS LF 清洁方式
- react-native - 有没有办法在不添加视图的情况下通过桥进行通信?
- javascript - OpenLayers 添加交互选择和翻译
- android - Android YUV_420_888输出灰色U和V通道(应该是彩色)
- templates - 如何获得由循环创建的类似资源的连接名称和真实名称的最终资源列表?