I am using Emailjs to handle my email deliveribility but I have an issue when it comes to recatcha verification.
The HTML form works well without the verification but once I activate the recaptcha in emailjs, I will no more get the notification in my email when the form is filled. Emailjs doc says:
“To add CAPTCHA support:
-
Create reCaptcha(opens new window) account or login to the existing account.
-
Register a new site and add your site domain to the list of domains. If you want to test the template in JSFiddle, please also add jsfiddle.net to the list.
-
Follow the “client-side integration” instructions as specified in the reCaptcha dashboard. If you are using the send method, please pass the reCaptcha token in the g-recaptcha-response property.
-
Open your template in the EmailJS template editor, go to Settings tab, and check Enable reCAPTCHA V2 verification checkbox.
-
Specify the secret key obtained from reCaptcha dashboard.
After the above steps have been completed it won’t be possible to send out an email based on this template without solving a CAPTCHA test first. No additional changes are required – we will automatically send the solved CAPTCHA result along with the send email request.”
The issue is, I am just a beginner in javascript and I don’t seem to understand how to pass the recaptcha token.
My codes are below.
The form
<head>
<script src="https://www.google.com/recaptcha/api.js"></script>
<!--recaptcha-->
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/@emailjs/browser@3/dist/email.min.js">
</script>
<!--emailjs-->
<script type="text/javascript">
(function(){
emailjs.init("EMAILJS_KEY");
})();
</script>
</head>
<div class="form-container">
<h2>Contact Me</h2>
<p>Feel free to contact me and I will get back to you as soon as possible.</p>
<div class="row">
<div class="col-lg-6">
<form method="POST" id="shev-form">
<input type="text" name="name" id="name_id" placeholder="Name" required>
<input type="email" name="email" id="email_id" placeholder="Email" required>
<input type="text" name="subject" id="subject_id" placeholder="Subject" required>
<textarea name="message" cols="30" rows="5" id="message_id" placeholder="Message" required></textarea>
<div class="g-recaptcha" data-sitekey="MY_SITE_KEY"></div>
<input type="submit" value="Submit" id="submit_id" onclick="sendMail()">
</form>
</div>
The javascript:
function sendMail() {
var params = {
name: document.getElementById("name_id").value,
email: document.getElementById("email_id").value,
subject: document.getElementById("subject_id").value,
message: document.getElementById("message_id").value,
};
const serviceID = "service_ID";
const templateID = "template_ID";
emailjs.send(serviceID, templateID, params)
.then(res=>{
document.getElementById("name_id").value = "";
document.getElementById("email_id").value = "";
document.getElementById("subject_id").value = "";
document.getElementById("message_id").value = "";
console.log(res);
alert("Your message sent successfully!!")
})
.catch(err=>console.log(err));
}
If the recaptcha is not sending a valid response to emailjs, the email will not be submitted.
Kindly help.
Thanks.