10

I'm integrating sendgrid in my node.js project with the following code as per instructions on their website

const sgMail = require('@sendgrid/mail')

const sendGridAPIKey = "API key"

sgMail.setApiKey(sendGridAPIKey)

const msg = {
 to: 'agrawalanuj751997@gmail.com',
 from: 'agrawalanuj751997@gmail.com',
 subject:'My first mail from node',
 text:"I'm sending myself an email"
}

sgMail.send(msg)

I'm getting the following error in my log. I've tried multiple API keys from multiple accounts but still, I'm getting the same error.

(node:16043) UnhandledPromiseRejectionWarning: Error: Forbidden
at Request._callback (node_modules/@sendgrid/client/src/classes/client.js:124:25)
at Request.self.callback (node_modules/request/request.js:185:22)
at Request.emit (events.js:200:13)
at Request.<anonymous> (node_modules/request/request.js:1154:10)
at Request.emit (events.js:200:13)
at IncomingMessage.<anonymous> (node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:288:20)
at IncomingMessage.emit (events.js:205:15)
at endReadableNT (_stream_readable.js:1154:12)
at processTicksAndRejections (internal/process/task_queues.js:84:9)
(node:16043) 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(). (rejection id: 1)
(node:16043) [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.
Anuj Agrawal
  • 101
  • 1
  • 6

3 Answers3

18

I was also facing the similar issue. I think they need to update the docs. The send method returns a promise that you have not handled that's why you're getting the error.

change

sgMail.send(msg)

to

sgMail.send(msg).then(() => {
    console.log('Message sent')
}).catch((error) => {
    console.log(error.response.body)
    // console.log(error.response.body.errors[0].message)
})

Now, the unhandled promise rejection error will be gone, And you'll get the error why the promise was being rejected.

Something like

The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements

It's pretty self-explanatory. Go to the specified link, and it'll guide you to verify the self identity. Once you complete that, It should work fine.


Links https://sendgrid.com/docs/ui/sending-email/sender-verification/

Aniket Kariya
  • 1,471
  • 2
  • 21
  • 26
  • That worked, but after verification even when I removed the promise why I did not get an Unhandled promise rejection warning? – Anuj Agrawal Apr 13 '20 at 21:22
  • That's because handling the resolve or reject is optional in promise. so it's okay to omit then..catch block it won't affect anything in your application if your method works successfully. The only down side of omitting that is if your promise rejects, there will be no catch block to handle that and you will end up getting untraceable error message on the console. – Aniket Kariya Apr 14 '20 at 06:23
0

I was also facing a similar issue. The send method returns a promise that you have not handled that's why you're getting the error.

sgMail.send({  
    to: 'arjunregmi148@gmail.com',
    from: 'arjunregmi148@gmail.com',
    subject: 'This is my first creation',
    text:'Be safe from corona virus'
}).then(() => {
    console.log('Message sent')
}).catch((error) => {
    console.log(error.response.body)
})
Khoa Nguyen
  • 1,319
  • 7
  • 21
-2

you can't verify user verify single sender on sendgrid

Yogesh Mishra
  • 13
  • 2
  • 5