0
app.post('/users/update',async(req,res)=>{
    try{
        const user = await User.findOne({email:req.body.email});
        if(!user)
        {
            return res.send({message:"Email ID not registered!"})
        }
        // SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA
        sgMail.setApiKey('SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA');
        const msg = {
            to: user.email,
            from: 'kartiken@gmail.com',
            subject: 'Validate Your Account',
            text: 'Hello User, here is your OTP: ',
            html: '<strong>21345</strong>',
            };
        sgMail.send(msg)
        .then(()=>{res.render('forgotPass2',{user})})
        .catch((e)=>{console.log('error',e)})
        console.log('Check mail')

    }catch(e){
        res.status(400).send(e)
    }
})

I am really tired of searching for a solution to this problem. The mail is never sent and I keep getting an error everytime after whatever I do. The error is caught by the promise.. I am attaching the error here!

Check mail
error ResponseError: Forbidden
    at C:\Users\Niranjan\Desktop\krb\web d\node\login-system\node_modules\@sendgrid\client\src\classes\client.js:133:29
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 403,
  response: {
    headers: {
      server: 'nginx',
      date: 'Tue, 14 Jul 2020 20:28:08 GMT',
      'content-type': 'application/json',
      'content-length': '281',
      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] }
  }
}

Pls help me with this so that I can move further...

Krab
  • 23
  • 3
  • Maybe the error message is listed inside the [Array]. Have you tried to read the values inside it? Something like: .catch((e)=>{console.log('error',e.body.errors)}) – ofri cofri Jul 14 '20 at 21:01
  • I am happy that i found a better substitute i.e nodemailer npm package. This has made it easy! src: https://www.w3schools.com/nodejs/nodejs_email.asp Thanks Anyway! – Krab Jul 15 '20 at 15:46

2 Answers2

0

from this link in documentation says:

https://github.com/sendgrid/sendgrid-nodejs/tree/master/packages/mail

Verify Sender Identity

"Verify an email address or domain in the Sender Authentication tab. Without this you will receive a 403 Forbidden response when trying to send mail."

after you logged into your account

https://app.sendgrid.com/settings/sender_auth/senders

and btw code looks fine

Eric Aska
  • 611
  • 5
  • 14
0

You have to create and verify Single Sender Verification from Send Grid in order to release out emails