sendgrid-nodejs
sendgrid-nodejs copied to clipboard
No response from send function if attachment exists
Issue Summary
If I try to send an email with attachment added, I get not Promise resolve and no response, However, if I comment out the attachment logic, I receive error, like I expected.
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
Hi @orassayag , I've tested the code that is not working as expected using node version v14.15.1 and an invalid api key. I still see the unauthorized (401) and null results errors printing to the console. Since I am unable to reproduce the error you are seeing with the attachment, I believe this is an issue with the file that you are attaching. The error could be happening because the total size of your email, including attachments, is larger than 30MB.
Hi @JenniferMah, Thanks for the reply. The file I'm trying to send is a doc file with 28KB size. I can't attach it here because GitHub not support attaching doc files. I saw your previous answer about the supported node versions, and I also tried to downgrade my node version to 8, and I still got this behavior. Try to reproducer the behavior with small doc file, with the code:
const pathToAttachment = 'C:\\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'
}
]
};
Update: You can download the specific doc file I'm trying to attach here: https://www.jumbomail.me/en/Downloads.aspx?sid=346F75534565524855507A464B3342347436653838673D3D
Hope you got it.
@orassayag
I've downloaded the file that you are trying to attach and I'm still unable to reproduce the lack of error messages that you were initially seeing. It looks like in the most recent code snippet the sending functionality is missing. Below I have posted the code that I've been using to try to reproduce the issue issue on Node v8.17.0 & Sendgrid/mail: 7.4.0. Let me know if you are still getting no response when an attachment exists with the code below.
const fs = require('fs');
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('SG.XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXXX');
const pathToAttachment = 'C:\\file.doc'; //absolute path to document
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'
},
],
};
(async () => {
try {
await sgMail.send(msg);
} catch (error) {
console.error(error);
if (error.response) {
console.log(typeof error.response.body)
}
}
})();
Thanks for the reply Jennifer. I took your code, and simulated on my local machine. Remember that I'm using Node 14.15.1. I took screen shots that you can will be able to see:
WORKING:

NOT WORKING:

@orassayag
Thanks for providing more information. I was able to recreate this issue and it looks like a bug on our end. We will add it to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog. In the meantime I would suggest using node version 12.20.0 and Sendgrid/mail: 7.4.0 as a work around solution for now. Using these versions, I was able to get the error logs to show up when attaching the same file.
Glad to help :) Don't really know what to do with "Pull requests and +1s on the issue summary will help it move up the backlog.", I'm just a simple developer who reports a bug :) If you need something else from me let me know. Thanks!
Facing similar issues when I have attachments. I have tried downgrading to v7.4.0 but it still doesnt work. This is what I am getting

this seems to be still the case. Adding attachment cause the email not to be received but no error reported.
Removing the following part I receive the email
attachments: [
{
content: qrcode,
filename,
disposition: 'attachment'
},
]