【问题标题】:Problem while dealing with sendgrid API with Node.js使用 Node.js 处理 sendgrid API 时出现问题
【发布时间】:2020-07-14 20:43:39
【问题描述】:
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)
    }
})

我真的厌倦了寻找解决这个问题的方法。邮件永远不会发送,并且每次我做任何事情后都会收到错误消息。错误已被承诺捕获。我在此处附上错误!

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] }
  }
}

请帮我解决这个问题,以便我可以更进一步......

【问题讨论】:

  • 可能错误信息列在 [Array] 中。您是否尝试过读取其中的值?类似: .catch((e)=>{console.log('error',e.body.errors)})
  • 我很高兴找到了更好的替代品,即 nodemailer npm 包。这很容易! src:w3schools.com/nodejs/nodejs_email.asp 无论如何谢谢!

标签: node.js sendgrid unhandled


【解决方案1】:

从文档中的这个链接说:

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

验证发件人身份

“在发件人身份验证选项卡中验证电子邮件地址或域。如果没有这个,您将在尝试发送邮件时收到 403 Forbidden 响应。”

在您登录帐户后

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

顺便说一句,代码看起来不错

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2022-11-15
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多