Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

node.js - unable to send email using 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 
    
   

ResponseError: Forbidden at D: ode-projectsecom [email protected]:146:29 at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 403, response: { headers: { server: 'nginx', date: 'Thu, 07 Jan 2021 11:55:26 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] } } }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The log said it failed because it received a Forbidden html response.
Try to check if you have set your Sender Authentication in your SendGrid Settings.
Only if you've set this done, you can send your email out.

References: https://sendgrid.com/docs/for-developers/sending-email/sender-identity/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...