Reputation: 527
I trying to implement a contact form on Nodejs and express with SendGrid but it's giving me 403 Forbidden
error, but the post request i sent returns 200
. i don't know what i'm doing wrong, please i need a help to fix this.
Here is my whole route
const express = require('express')
const router = express.Router()
const ContacForm = require('../models/contact_form')
const fs = require('fs')
const path = require('path')
const sgMail = require('@sendgrid/mail');
const nodemailer = require("nodemailer");
router.get('/new', (req, res) => {
res.render("contact_form/new")
})
router.post('/', (req, res) => {
const output = `
<p>You have a new Request</p>
<h3>Contact Details </h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
/ul>
<h3>Message</h3>
<li>Request: ${req.body.request}</li>
`;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: output,
};
sgMail.send(msg, (error, contact)=> {
if(error) {
console.log(error)
res.render("contact_form/new")
}
});
});
and these is the error message response i get on the terminal
ResponseError: Forbidden
at node_modules/@sendgrid/client/src/classes/client.js:105:29
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 403,
message: 'Forbidden',
response: {
headers: {
server: 'nginx',
date: 'Sat, 11 Apr 2020 13:15:25 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] }
}
}
and here is my form
<form method="POST" action="/contact_form">
<label>Name</label>
<input type="text" name="name" id="name" placeholder="Enter Your Name">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Enter Your Email">
<label>Request</label>
<textarea name="request" id="request" placeholder="Enter Your Prayer Request" cols="30"
rows="10"></textarea>
<button type="submit"> Submit </button>
</form>
NOTE: I'm sending my SENDGRID_API_KEY
variable and it's coming through
This is the kind of Sendgrid API that i am using
Integrate using our Web API or SMTP Relay
Upvotes: 0
Views: 3693
Reputation: 527
I just figured it out after taking time to research and read through the documentation.
It happened that I needed to do additional authentication called Single Sender Verification
etc.
and I changed my code to give me a better understanding of the error
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Hello world',
text: output
};
sgMail
.send(msg)
.then(() => {
//Celebrate
console.log('Email Sent!');
})
.catch(error => {
//Log friendly error
console.error(error.toString());
console.log(output)
//Extract error msg
const {message, code, response} = error;
//Extract response msg
const {headers, body} = response;
});
});
after changing the code, the error message changed to
Forbidden (403)
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
from this error, I read through the error docs on send-grids.
I think the additional authentication was added last month.
Upvotes: 4
Reputation: 1
I had the same problem, the sendgrid from April 6, 2020 changed some criteria for authentication of free tests :(. Now it is necessary to make some configurations. What solved for me was to follow this tutorial here from them. https://sendgrid.com/docs/ui/sending-email/sender-verification/. Only authorizing a few emails to send requests
Upvotes: 0
Reputation: 94
Your post request is returning 200 because you call res.render("contact_form/new")
when there IS an error:
if(error) {
console.log(error)
res.render("contact_form/new")
}
Take a look at the body of the response you get from the SendGrid response. It contains an array of errors that may provide more information on why you're receiving a 403.
Upvotes: 0