I'm relatively new to using sendgrid with node.js, and wanted to check it out. I followed all the instructions provided on https://app.sendgrid.com/guide/integrate/langs/nodejs. I even tried to replacing the "to" and "from" properties of "msg" with my own email id, and got the same error as the result.
1.Created a new account and generated API Key over at https://app.sendgrid.com/guide/integrate/langs/nodejs
2.Followed steps 2,3,4
3..Tried to implement step 5, and the below mentioned error was encountered
4..In order to get the log of the error, I set up a try-catch statement to work as a promise handler which would output the error 5.log, if there was an error.
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey("xxxxxxxxx-xxx-xxxxx");
//routes
app.use('/', indexRouter);
//contact form
app.post('/send', (req,res)=>{
const msg = {
to: '[email protected]',
from: '[email protected]',
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>',
}
sgMail. send(msg);
});
```
(node:8248) UnhandledPromiseRejectionWarning: Error: Forbidden
at C:\Users\Derry-uk\Desktop\work space\projects\01 portfolio website\v1\node_modules\@sendgrid\clientsrc\classes\client.js:133:29
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:8248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:8248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
POST /send - - ms - -
Hello @Derry-ukere,
Could you please provide the code which contains your try-catch logic so that I may try to reproduce? Thanks!
With best regards,
Elmer
It's a documentation issue, since the page doesn't mention that you need to use a verified sender, and ideally a recipient. Since there is a validation step that interrogates logs, it would seem reasonable to use the example.com email addresses provided. Not so.
To set up a verified sender, see: https://app.sendgrid.com/settings/sender_auth
Incidentally, you might find that that once the test mail does send, that it lands in spam (at least it did for me in gmail).
@thinkingserious Regarding the try/catch error - it's coming from sendgrid-nodejs. You get the same error using the script provided here: https://app.sendgrid.com/guide/integrate/langs/nodejs
The UnhandledPromiseRejectionWarning is likely due to the code provided not being wrapped in a try/catch block or not resolving the Promise returned by sgMail.send(msg); A straightforward way to resolve this issue would be use .then(...).catch(...);. Please see our guide on handing the Promise returned by send for further help.
@eshanholtz That sounds like a second opportunity to improve the page at https://app.sendgrid.com/guide/integrate/langs/nodejs (in addition to clarifying the need for a verified sender or domain).:
// using Twilio SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
- from: '[email protected]',
+ from: '[email protected]', // Replace with a verified sender
subject: 'Sending with Twilio 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>',
};
-sgMail.send(msg);
+ sgMail.send(msg)
+ .then(() => {
+ console.log("Message sent");
+ })
+ .catch(error => {
+ console.error(error);
+ };
...or similar.
Thank you for the documentation update suggestions @mbrookes! I have forwarded them to our documentation team.
@thinkingserious How did you do that?
@childish-sambino - @thinkingserious works for Twilio (SendGrid owner).
Presumably the issue can also be labelled as 'type: docs update'.
@mbrookes So do I. Just trying to figure out the proper protocol.
Oops! 馃槅
@childish-sambino I put a request in via the AirTable form. Once I obtain a tracking ID, I will post here.
Hi All,
Thanks for looping in the docs team. We have updated the code samples for that integration guide in the app here:
GitHub Gist: https://gist.github.com/sendgrid-gists/069d788aefb7853706f424cbdfd7ee3c
And we just pushed up a PR to our Node.js quickstart in the docs here:
PR: https://github.com/sendgrid/docs/pull/6255
Docs Page: https://sendgrid.com/docs/for-developers/sending-email/quickstart-nodejs/
They should both have inline comments to help make it clear that the "From" address should change to a verified address. They also now have a catch( error => {console.error(error)} ) on the send() method. The quickstart has more explanation about the need for a verified address and links to our domain authentication docs. I hope this all helps.
Looks good. Glad to have helped. 馃檪
Most helpful comment
Hi All,
Thanks for looping in the docs team. We have updated the code samples for that integration guide in the app here:
GitHub Gist: https://gist.github.com/sendgrid-gists/069d788aefb7853706f424cbdfd7ee3c
And we just pushed up a PR to our Node.js quickstart in the docs here:
PR: https://github.com/sendgrid/docs/pull/6255
Docs Page: https://sendgrid.com/docs/for-developers/sending-email/quickstart-nodejs/
They should both have inline comments to help make it clear that the "From" address should change to a verified address. They also now have a
catch( error => {console.error(error)} )on thesend()method. The quickstart has more explanation about the need for a verified address and links to our domain authentication docs. I hope this all helps.