Issue Summary
If I try to send an email with attachment added, I get no Promise resolve and no response, However, if I comment out the attachment logic, I receive an error (I entered invalid token to get an error), like I expected. The path to the doc file is real, but for this piece of code I changed it.
Code not working:
const fs = require('fs');
const sgMail = require('@sendgrid/mail');
(async () => {
sgMail.setApiKey('SG.XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXXX');
const pathToAttachment = 'Path\\To\\doc\\file.doc';
const attachment = fs.readFileSync(pathToAttachment).toString('base64');
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'test',
text: 'test',
attachments: [
{
content: attachment,
filename: 'file.doc',
type: 'application/doc',
disposition: 'attachment'
}
]
};
let result = null;
try {
result = await sgMail.send(msg);
console.log('sent!');
} catch (err) {
console.error(err.toString());
}
console.log(`result: ${result}`);
})();
Not getting any response, the code ignores the rest of any code that goes after the 'send' function.
Code working:
const fs = require('fs');
const sgMail = require('@sendgrid/mail');
(async () => {
sgMail.setApiKey('SG.XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXXX');
const pathToAttachment = 'Path\\To\\doc\\file.doc';
const attachment = fs.readFileSync(pathToAttachment).toString('base64');
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'test',
text: 'test',
/* attachments: [
{
content: attachment,
filename: 'file.doc',
type: 'application/doc',
disposition: 'attachment'
}
] */
};
let result = null;
try {
result = await sgMail.send(msg);
console.log('sent!');
} catch (err) {
console.error(err.toString());
}
console.log(`result: ${result}`);
})();
Code works as I expected, getting:
Unauthorized (401)
The provided authorization grant is invalid, expired, or revoked
null
null
result: null
Technical details:
"@sendgrid/mail": "^7.4.0"
node version: v14.15.1
I posted an issue on this on the their GitHub page:
https://github.com/sendgrid/sendgrid-nodejs/issues/1220
But they seem not to be able to produce the bug.
Has anyone else faced this issue before?
As I write to SendGrid developers, they admit it's a bug on their package and it will be fixed:
Question close.
here