I am currently using Azure Functions and node.js with Sendgrid to email users when blobs containing specified PDF's arrive in our blob repository. This has been fine, however I'm now trying to attach the PDF itself to the email. Every time I've tried this, the PDF has been attached to the email but won't open (says its corrupted). This is essentially this issue (Azure Function (JS) using SendGrid with attachment), but I cant comment to ask then directly since I'm a new user.
But I dont understand how to set the content for the attachment.
Here's my code:
var util = require('util');
module.exports = function (context, myBlob) {
var message = {
"personalizations": [ { "to": [ { "email": "[email protected]" } ] } ],
from: { email: "[email protected]" },
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);
};
other input I've tried in the content variable: myBlob, new Buffer (context.bindings.myBlob, "base64"),"base64". Any help would be appreciated, thanks in advance for your time.
To use base64 encode, you need to change your input blob datatype to binary first.
add "dataType": "binary" in your function.json blob trigger bindings.
Then encode your PDF to base64 string,
content: new Buffer(myBlob).toString('base64')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With