Sendgrid-nodejs: How to attach excel file

Created on 1 Dec 2017  路  8Comments  路  Source: sendgrid/sendgrid-nodejs

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
}
};

help wanted question

Most helpful comment

Thank you, It's working

All 8 comments

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. (c:Users\Spandana\Documents\Projects\CashfloCoreAPIs\node_modules\request\request.js:1163:10)\n at emitOne (events.js:115:13)\n at Request.emit (events.js:210:7)\n at IncomingMessage. (c:Users\Spandana\Documents\Projects\CashfloCoreAPIs\node_modules\request\request.js:1085:12)\n at Object.onceWrapper (events.js:314:30)\n at emitNone (events.js:110:20)\n at IncomingMessage.emit (events.js:207:7)\n at endReadableNT (_stream_readable.js:1057:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickDomainCallback (internal/process/next_tick.js:218:9)"
__proto__:Error {constructor: , toString: , toJSON: }

Can you tell me what I'm doing wrong

Hi @Spandana-Gajangi,

Could you please grab the error message, like so?

Thanks!

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thinkingserious picture thinkingserious  路  4Comments

thinkingserious picture thinkingserious  路  4Comments

wooyah picture wooyah  路  4Comments

amlcodes picture amlcodes  路  4Comments

metalshan picture metalshan  路  3Comments