Reputation: 75
I want to send emails with attachment from an Azure function (Javascript) using SendGrid. I have done the following
Following is my Azure Function
module.exports = function (context, myQueueItem) {
var message = {
"personalizations": [ { "to": [ { "email": "[email protected]" } ] } ],
from: { email: "[email protected]" },
subject: "Azure news",
content: [{
type: 'text/plain',
value: myQueueItem
}]
};
context.done(null, {message});
};
Email is getting send correctly. But how do i add an attachment?
Upvotes: 1
Views: 1073
Reputation: 5008
You can try following snippet from Sendgrid API:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Hello attachment',
html: '<p>Here’s an attachment for you!</p>',
attachments: [
{
content: 'Some base 64 encoded attachment content',
filename: 'some-attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
},
],
};
So in your context:
module.exports = function (context, myQueueItem) {
var message = {
"personalizations": [ { "to": [ { "email": "[email protected]" } ] } ],
from: { email: "[email protected]" },
subject: "Azure news",
content: [{
type: 'text/plain',
value: myQueueItem
}],
attachments: [
{
content: 'Some base 64 encoded attachment content',
filename: 'some-attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
},
]
};
context.done(null, {message});
};
Upvotes: 1