SendGrid No response from send function if attachment exists

517 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

As I write to SendGrid developers, they admit it's a bug on their package and it will be fixed:

"@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."

Question close.

here