converting database date to excel. How to attach this excel data(not exported as file) to an email.
I'm able to send email but file is not attached.
here is my code,
module.exports.sendEmailTemplate = async (templateId, tos, substitutions, data) => {
sendgrid.setApiKey(process.env.SENDGRID_API_KEY);
if (tos.length === 2 && tos[0].email === tos[1].email) {
tos.splice(1, 1);
}
const email = {
from: { name: 'xxxxxx', email: 'xxxxxxx' },
to: tos,
templateId,
substitutions,
substitutionWrappers: ['_', '_'],
};
if (data) {
email.file ={
filename: 'sample.xlsx',
content: data,
contentType: 'text',
};
}
try {
await sendgrid.send(email);
} catch (error) {
throw error
}
};
Hello @Spandana-Gajangi,
Here is the documentation on how to include attachments.
My guess is that your data is not base64 encoded.
With Best Regards,
Elmer
attachments= [
{
content: data.toString('base64'),
filename: 'Invoices.xlsx',
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
},
]
RESPONSE:
ResponseError: Bad Request
code:400
message:"Bad Request"
response:Object {headers: Object, body: Object}
stack:"Error: Bad Request\n at Request.http [as _callback] (c:Users\Spandana\Documents\Projects\CashfloCoreAPIs\node_modules\@sendgridclient\srcclient.js:124:25)\n at Request.self.callback (c:Users\Spandana\Documents\Projects\CashfloCoreAPIs\node_modules\request\request.js:186:22)\n at emitTwo (events.js:125:13)\n at Request.emit (events.js:213:7)\n at Request.
__proto__:Error {constructor: , toString: , toJSON: }
Can you tell me what I'm doing wrong
Bad Request (400)
The attachment content must be base64 encoded.
attachments.0.content
http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.content
Hi @Spandana-Gajangi,
This means that your data variable is not base64 encoded.
You should be able to do:
new Buffer(data).toString('base64')
Thank you, It's working
Hello @thinkingserious , How may I attach excel file using sendgrid v2? Thanks :)
Hello @SienaSantos,
Here is the documentation we have for attachments for v2 of the SendGrid API using version 1.9.2 of this SDK.
That said, I would recommend updating to the latest version of this SDK, which supports v3 of the SendGrid API.
With Best Regards,
Elmer
Most helpful comment
Thank you, It's working