Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No response from send function if attachment exists #1220

Open
orassayag opened this issue Nov 21, 2020 · 7 comments
Open

No response from send function if attachment exists #1220

orassayag opened this issue Nov 21, 2020 · 7 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@orassayag
Copy link

orassayag commented Nov 21, 2020

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
@JenniferMah JenniferMah added the type: getting started question while getting started label Nov 25, 2020
@JenniferMah
Copy link
Contributor

JenniferMah commented Nov 25, 2020

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.

@JenniferMah JenniferMah added the status: waiting for feedback waiting for feedback from the submitter label Nov 25, 2020
@orassayag
Copy link
Author

orassayag commented Nov 25, 2020

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.

@JenniferMah
Copy link
Contributor

JenniferMah commented Dec 3, 2020

@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)
        }
    }
})();

@orassayag
Copy link
Author

orassayag commented Dec 3, 2020

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:
works

NOT WORKING:
not working

@JenniferMah
Copy link
Contributor

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

@JenniferMah JenniferMah added type: bug bug in the library and removed type: getting started question while getting started labels Dec 4, 2020
@orassayag
Copy link
Author

orassayag commented Dec 4, 2020

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!

@JenniferMah JenniferMah added status: help wanted requesting help from the community and removed status: waiting for feedback waiting for feedback from the submitter labels Dec 4, 2020
@kayeew
Copy link

kayeew commented Oct 23, 2021

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

3 participants