您好,我正在尝试通过电子邮件发送 pdf 附件。
sgMail.setApiKey(my_key);
fs.readFile('pdf/4.pdf', function(err, data) {
sgMail.send({
to : 'dinesh@mail.com',
from : 'xxxxxxxxx@jdk.com',
subject : 'Report',
attachments : [{filename: 'Report.pdf',
content: data,
type: 'application/pdf',
}],
html : 'bla bla'})})
Run Code Online (Sandbox Code Playgroud)
这段代码给了我一个错误
Invalid type. Expected: string, given: object.
Run Code Online (Sandbox Code Playgroud)
但我通过另一个 stackoverflow 答案找到了这个代码片段。它表示您不需要传递内容的 uri 编码数据。(评论里的这个问题)
如何使用 Node.js 实现这一点?
小智 7
我确实将我的编码为 Base64,这就是我的做法。也许这有帮助。
function base64_encode(file){
let bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
}
let data_base64 = base64_encode('../invoice.pdf');
const msg = {
to: emails,
from: '-----.com',
subject: `Invoice`,
text: `Invoice`,
html: "whatever",
attachments: [
{
filename: `invoice`,
content: data_base64,
type: 'application/pdf',
disposition: 'attachment'
}
]
};
sgMail
.send(msg)
.then((response) => {
res.status(200).send('Success');
})
.catch((err) => {
res.status(500).send(err);
});
Run Code Online (Sandbox Code Playgroud)
希望这对其他人有帮助。使用@sendgrid/mail": "^6.3.1",
归档时间: |
|
查看次数: |
3075 次 |
最近记录: |