无法使用 sendgrid 发送电子邮件

时间:2021-01-07 12:00:41

标签: node.js sendgrid

   // this is my code
//unable to send email using sendgrid
// finding email from body
    exports.signUp=async (req,res)=>{
        const userExists = await User.findOne({ email: req.body.email })
        if(userExists){
            return res.status(400).json({
                error:"eamil is already taken"
            })
        }
        const token = jwt.sign(req.body, process.env.ACCOUNT_ACTIVATION,{expiresIn:'20m'})
        const emailSend = {
            from: process.env.EMAIL_FROM,
            to: req.body.email,
            subject:`Acoount activation link`,
            html:`
                <p> Please click the following link to activate</p>
    
                <a>http://localhost:8000/api/auth/activate/${token}</a>
            `
        }
        sgMail.send(emailSend).then(sent=>{
            return res.status(200).json({
                message:'email has been sent to you emailid'
            })
        })
        .catch(err=>console.log(err))
    }
    
    
    
   // this is error am getting
// am getting this error 
    
   
<块引用>

响应错误:禁止 在 D:\node-projects\ecom\node_modules@sendgrid\client\src\classes\client.js:146:29 在 processTicksAndRejections (internal/process/task_queues.js:93:5) {
代码:403, 回复: { 标题:{ 服务器:'nginx', 日期:'星期四,2021 年 1 月 7 日 11:55:26 GMT', '内容类型':'应用程序/json', '内容长度':'281', 连接:'关闭', 'access-control-allow-origin': 'https://sendgrid.api-docs.io', '访问控制允许方法':'POST', 'access-control-allow-headers':'授权,内容类型,代表,x-sg-elas-acl', “访问控制最大年龄”:“600”, 'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' }, 正文:{错误:[数组]} } }

1 个答案:

答案 0 :(得分:-1)

日志说它失败了,因为它收到了一个 Forbidden html 响应。
尝试检查您是否在 SendGrid 设置中设置了 Sender Authentication
只有在您完成此设置后,您才能将电子邮件发送出去。

参考文献:https://sendgrid.com/docs/for-developers/sending-email/sender-identity/

相关问题