-
Notifications
You must be signed in to change notification settings - Fork 779
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
Send bulk mail get error 401 Unauthorized #970
Comments
Hi, I have a very similiar issues with the SDK here (Using NodeJS SDK 6.4.0), and customer support sent me here as they think it's an issue from the SDK itself. I often send emails at the same time, from the same server, but got different answer from the API calls. Exemple :
Here are some figures about one day, when we tried to send multiple email using the SDK to call SendGrid :
Can there be concurrency issues with the SDK when trying multiple calls with the same API Key ? Here is the code used : const sgMail = require('@sendgrid/mail');
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
const SENDGRID_MY_TEMPLATE_ID = process.env.SENDGRID_MY_TEMPLATE_ID;
sgMail.setApiKey(SENDGRID_API_KEY);
class Mail {
static async sendReport(recipientEmails, data) {
const email = {
to: recipientEmails,
bcc: ['[email protected]', '[email protected]'],
from: {
email: '[email protected]',
name: 'MyCompany',
},
replyTo: '[email protected]',
template_id: SENDGRID_MY_TEMPLATE_ID,
asm: {
group_id: 1234,
},
dynamic_template_data: data,
};
try {
await sgMail.sendMultiple(email);
} catch (e) {
console.log('ERROR Sending Report', e);
console.log('ERROR Sending Report', e.toString());
console.log('ERROR Sending Report', e.response.body);
throw e;
}
}
} |
I confirm that it's the same for me. Some request are accepted, but others get 401 unauthorized. |
I tried multiple changes :
const sgMail = require('@sendgrid/mail');
const SENDGRID_MY_TEMPLATE_ID = process.env.SENDGRID_MY_TEMPLATE_ID;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
sgMail.setApiKey('HARDCODED_API_KEY');
class Mail {
static async sendReport(recipientEmails, data) {
const email = {
to: recipientEmails,
bcc: ['[email protected]', '[email protected]'],
from: {
email: '[email protected]',
name: 'MyCompany',
},
replyTo: '[email protected]',
template_id: SENDGRID_MY_TEMPLATE_ID,
asm: {
group_id: 1234,
},
dynamic_template_data: data,
};
try {
await sgMail.send(email);
await sleep(500);
} catch (e) {
console.log('ERROR Sending Report', e);
console.log('ERROR Sending Report', e.toString());
console.log('ERROR Sending Report', e.response.body);
throw e;
}
}
} But same issue ... What is strange is that the corresponding source code seems clean, so kind of difficult to troubleshoot without the help of SendGrid devs accessing the API code ... |
@yoagauthier Did you manage to fix the issue? |
@francesco-clementi-bip No, and we'll probably switch mail provider very soon, this repo seems quite dead and support is not very helpful understanding the issue. I'm not sure anymore the issue comes from the SDK though : I've tried to code directly against the REST API, and I still have API calls to My code : const axios = require('axios');
const {
SENDGRID_MY_TEMPLATE_ID,
SENDGRID_API_KEY,
} = process.env;
class SendGridClient {
constructor() {
this.axiosInstance = axios.create({
baseURL: 'https://api.sendgrid.com/v3',
headers: {
Authorization: `Bearer ${SENDGRID_API_KEY}`,
},
});
}
async sendMail(toEmail, data) {
const email = {
personalizations: [
{
to: [
{
email: toEmail,
},
],
dynamic_template_data: data,
},
],
from: {
email: '[email protected]',
name: 'MyCompany',
},
reply_to: {
email: '[email protected]',
name: 'MyCompany',
},
template_id: SENDGRID_MY_TEMPLATE_ID,
asm: {
group_id: 1234,
},
};
try {
await this.axiosInstance.post('/mail/send', email);
} catch (e) {
console.log('ERROR Sending Report', e);
console.log('ERROR Sending Report', e.response.data);
throw e;
}
}
}
module.exports = SendGridClient; |
Thanks @yoagauthier , |
We're also having this issue randomly (maybe twice a month or so): |
Same problem. I used Sendgrid for couple of years. And recently started getting 401 error. Customer service is useless. |
Same here, just started getting these errors randomly while in dev-mode (low volume testing). Nothing changed just randomly started getting these errors. |
I got same problem, and I removed sendgrid const sgMail = require('@sendgrid/mail');
const SENDGRID_API_KEY='{api_key};
sgMail.setApiKey(SENDGRID_API_KEY); |
If you're seeing similar behaviour when accessing the API directly, then this library won't be able resolve the issue for you. That said, it's possible the API key is not being loaded properly based on your environment. Sticking the API key in a What we can do in this library is update the underlying client to throw an exception if you're setting the API key to an empty string which would indicate an issue with your environment and fail-fast to help debug the issue. I'll leave this issue open for anyone wanting to implement that last piece. |
Also, I've confirmed that API keys always start with the prefix |
+1 have the same issue when sending several emails to different receivers in a short time period. |
@childish-sambino is there a sure-fire way to avoid this issue? Perhaps there is a max number of emails that can be sent in the same |
@dockleryxk If you're seeing seemingly random |
Changing my previous comment about leaving this open to track handling empty/undefined/null/blank API keys. Let's use #1068 to track that work. |
@childish-sambino I cannot agree with your conclusion to close this issue. you asuming something, but do you have proof? more likely its an issue with this library. |
@crascher To help debug the issue you're seeing, have you verified (say, through logging) that you're sending the correct API key when you're seeing the |
@childish-sambino thanks for your response. To provide some more context, my API key is hardcoded (it doesn't matter in my case). I have tried to recreate the error but it never happens. It's only in a prod environment where I can't simply resend them because it would result in some annoyed customers. |
@childish-sambino I added inside the callback method of sendMail logic to execute another sendMail if an error is occured. The mail is sent to me and that is working. Are you planing to open this ticket again? For me its not solved yet. |
More context: We've been using this package for almost two years. February 25 was the first time this ever happened for us. Then March 15 and March 17 and now twice today. This issue isn't resolved and seems to be getting worse. |
@dockleryxk @crascher Sounds like the same issue: seemingly random My recommendation to open a support ticket still stands (support doesn't monitor issues here, engineers do). If you can provide them with details about when the request failed and any other details will help to track it down. If that doesn't help or you don't hear back in a timely manner, let me know. We're working to be more responsive in these forums. Lastly, the library will soon be migrating the underlying HTTP client (#1042) which we'll do a major version roll for. It's possible there was a bug in the current client so may be worth waiting for the changes to roll out to see if it resolves the issue. |
@childish-sambino I'll open a ticket then, thank you! If I end up resolving it with them I will post back here about it |
fwiw adding my comment here in case someone might hit it on different key words We're running several dynamic template tests using jest. We started running into the same random 401 issue this afternoon, after running the same suite of tests all morning without issue. I reduced the test group to 10 tests, and we're running them in jest sequentially (so there's not multiple Given the simplistic nature of the client's code, this looks more like a network issue or network limitation to me. |
the problem ist not solved yet; I am using latest version 7 and get sometimes: Unauthorized (401) Error: Unauthorized code: 401, Support says: Mar 24, 12:33 PM PDT Thanks for your reply and additional insight. I checked with our team regarding this issue. As it turns out, making rapid Mail Send calls in a row can intermittently lead to an odd 401 error. I sincerely apologize for any inconvenience caused. In this instance, we recommend implementing some form of retry logic within your code when you receive the 401 error as a workaround. Hopefully, this information was helpful. Please let me know if I was unable to answer your questions, or if you have any other questions or concerns. Best, When does it get fixed and why the issue ist not open if it is still an issue? |
It happened to me too, but i think my problem was different. Because I had already reached limit to of 100 mail/day nothing else. Bulk email is now sent perfectly without any errors. idk if it helps just wanted to share. |
I'm using @sendgrid/mail module on Google Cloud Functions.
My mails are formatted like this:
I have an array of mails with different "to" and different html emails.
When I use sgMail.send(emailsArray) and user.email is just me, then the mails will be sent so the api key is working.
When I use sgMail.send(emailsArray) and user.email are different between them, I get this error:
Any help?
Technical details:
"@sendgrid/mail": "^6.4.0",
Node.js Version: "^8.0.0"
The text was updated successfully, but these errors were encountered: