Closed JimLynchCodes closed 4 years ago
I'm also having this issue, and @eshanholtz that format didn't fix it for me. I'll switch to the REST API I suppose.
@trademark18 What version of the library and node are you using? Are you getting any errors from the API?
@eshanholtz I'm having similar issues with version: 7.2.1
. I'm not receiving any errors from the API. Tried a few different ways, some appear more successful than others (when looking at the SendGrid activity log). In all cases, the email is dropped, and the To
field appears blank within the log.
With a barebones install of 7.2.1, I've tried the following:
const sgMail = require('@sendgrid/mail')
const apiKey = 'SG.SOME_LONG_AND_VALID_API_KEY';
const templateId = 'd-6a7edba7c8114011914.................';
sgMail.setApiKey(apiKey);
// const msg = {
// personalizations : [{
// substitutions : {
// ":token" : 'fa9d9f8s8d98f9s'
// },
// subject : "Hello from Java",
// to : [{
// email : 'xxxxxxxxx@mac.com'
// }]
// }],
// from : {
// email : 'store@some-authorized-domain.com'
// },
// template_id : templateId
// };
// const msg = {
// to: 'xxxxxxxxx@mac.com',
// from: 'store@some-authorized-domain.com',
// templateId : templateId,
// dynamicTemplateData: {
// token: 'somelongtoken'
// }
// };
// const msg = {
// personalizations: [{
// to: 'xxxxxxxxx@mac.com',
// dynamic_template_data: {
// token: 'somelongtoken'
// }
// }],
// from: 'store@some-authorized-domain.com',
// templateId: templateId
// };
// const msg = {
// personalizations: [{
// to: 'xxxxxxxxx@mac.com',
// subject: 'hello michael',
// dynamic_template_data: {
// token: 'somelongtoken'
// }
// }],
// from: 'store@some-authorized-domain.com',
// templateId: templateId
// };
// const msg = {
// personalizations: [{
// to : [{
// email : 'xxxxxxxxx@mac.com'
// }],
// subject: 'hello michael 2',
// dynamic_template_data: {
// token: 'somelongtoken'
// }
// }],
// from: 'store@some-authorized-domain.com',
// templateId: templateId
// };
// const msg = {
// from: 'store@some-authorized-domain.com',
// template_id: templateId,
// personalizations: [{
// to: { email: 'xxxxxxxxxx@mac.com', },
// dynamic_template_data: {
// token: 'my-long-token'
// },
// }],
// }
// ONLY ONE THAT WORKS
const msg = {
to: 'xxxxxxxxx@mac.com',
from: 'store@some-authorized-domain.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
console.log( msg );
// Send it
sgMail.send(msg)
.then((result) => {
console.log( result );
})
.catch((error) => {
console.log( error );
});
Weird, I just tried with version 7.0.1, and I'm getting an error now that my key doesn't start with SG. even though it does...
const sgMail = require('@sendgrid/mail');
sgMail.setUpApiKey('SG.eDSDFGerkjwkjrehfqfg....');
Error: API key does not start with "SG."
Hello all,
I've tried reproducing the bug locally, with package versions 7.0.1
and 7.2.1
with no success. Here is my code snippet using templates that has been successfully sending emails:
sg = require('@sendgrid/mail');
sg.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'you@example.com',
from: 'me@authorized-domain.com',
templateId: 'd-baddb3551a76483598ece251aa7fae6a', // Make sure you're using your own template ID!
dynamic_template_data: {
subject: 'Testing Templates',
name: 'Someone',
city: 'Denver',
},
};
sg.send(msg).then((resp) => {
console.log(resp);
}).catch(err => {
console.log(err.response.body);
});
@JimLynchCodes The one thing I noticed is that templateId
in your original code snippet exactly matches the template ID in the example code. Please double check that you have substituted its value with your own template ID. As for why it's not reading your API key correctly, it's hard to tell without more information. Maybe try using an environment variable?
@msacket I noticed that you're not quite handling errors properly, which may be why you're not seeing errors. You'll want to look at error.response.body
to get the proper API error response. For more information, please take a look at the error handling docs I linked in my last comment. As an aside, the following msg
object formats also worked successfully for me:
const msg = {
personalizations: [{
to: 'you@example.com',
dynamic_template_data: {
subject: 'Testing Templates',
name: 'Someone',
city: 'Denver',
},
}],
from: 'me@authorized-domain.com',
templateId: 'd-baddb3551a76483598ece251aa7fae6a',
};
const msg = {
personalizations: [{
to: [{
email: 'you@example.com',
}],
dynamic_template_data: {
subject: 'Testing Templates',
name: 'Someone',
city: 'Denver',
},
}],
from: 'me@authorized-domain.com',
templateId: 'd-baddb3551a76483598ece251aa7fae6a', // also works with the key being `template_id`
};
As a final note, the Legacy Transactional Templates have been deprecated for Dynamic Templates. If your account was created recently, your API key will only give you access to Dynamic Templates.
@eshanholtz . Thanks for the feedback. Gave it a go... but no go unfortunately. I'll take a closer look, but part of the issue is that it never throws any kind of error. It always returns a 202. Email shows up in the activity log as dropped (always missing the to
field). The activity log details may
show the template or subject, depending on which version above I used.
I've check the following: √ Template exists (should be a new template as it was created this week) √ To email exists and is valid √ From email exists and is authorized √ API Key (full access for testing)
const msg = {
personalizations: [{
to: 'you@example.com',
dynamic_template_data: {
subject: 'Testing Templates',
name: 'Someone',
city: 'Denver',
},
}],
from: 'me@authorized-domain.com',
templateId: 'd-baddb3551a76483598ece251aa7fae6a',
};
sgMail.send(msg)
.then((result) => {
console.log( result );
})
.catch((error) => {
console.log( error.response.body );
});
I ended up just bypassing sending via templates altogether by exporting the template and using handlebars to compile the template into html and send it that way. Not ideal, but keeps me moving forward.
@msacket Since you're getting 202s and seeing email activity, this no longer sounds like a helper library issue. For your case, I would recommend reaching out to support for further help.
Hey @eshanholtz!
I have been trying this, but I am finding the same results as @msacket in that this lib doesn't work for dynamic templates.
It ONLY works when specifying subject and html:
const msg = {
to: 'xxxxxxxxx@mac.com',
from: 'store@some-authorized-domain.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
console.log( msg );
// Send it
sgMail.send(msg)
.then((result) => {
console.log( result );
})
.catch((error) => {
console.log( error );
});
I tried your code snippet with my own template-id @eshanholtz , but it always returns 401 in that case:
ResponseError: Unauthorized
at node_modules/@sendgrid/client/src/classes/client.js:133:29
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 401,
response: {
headers: {
server: 'nginx',
date: 'Mon, 06 Jul 2020 21:44:04 GMT',
'content-type': 'application/json',
'content-length': '88',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'
},
body: { errors: [Array] }
}
}
{
errors: [
{
message: 'Permission denied, wrong credentials',
field: null,
help: null
}
]
}
@JimLynchCodes The unauthorized error suggests that SendGrid is not able to validate your API key. I would suggest double checking that the contents of your API key environment variable is correct, or potentially regenerating a new API key. If you're still having issues with authorization errors after that, I would recommend reaching out to support for further account help.
Also, to see the full error message, you should be logging console.log(error.response.body)
@eshanholtz It is accepting my api key now.
I am using exactly your code but with my templateId.
I get no errors, but the email never arrives... have tried with multiple different email addresses... 🤔
@JimLynchCodes Try checking your email activity first for further clues and if you don't get anywhere, reach out to support
I only have 3 days available for email activity... bad place for a paywall. :'(
SMH
You'll have to try sending another test email so you can look at recent email activity. Please also verify that your template ID exists for the account you're using to send the email.
@msacket did you managed to solve the issue? I'm experiencing the same problem too...
I had this same behaviour when the dynamic email template was not set to 'active'. Setting the template to 'active' solved the problem for me.
Hi, I created a "transactional email" in the sendgrid dashboard. I am using the exact code snippet here but with my template id and my from email address:
When I completely remove the template reference in my msg object and just send some text I receive the email every time:
Is sendgrid transactional emails broken? Why is this not working for me?