I think I've now got this working, so I thought I would post my solution in case it helps anyone else trying to work out how to integrate Google reCaptcha v3 with Universal Email. I save these blocks as Snippets in Dreamweaver so that I can easily add them to pages.
First, generate your SITEKEY and SECRET KEY from Google reCaptcha.
Add your email script to the page first because it won't be available from the Server behaviors once the reCaptcha code is added.
Insert the following after the WA_Email.php include, changing SECRET_KEY to your own secret key. You can also change the sensitivity of the reCaptcha (0.5 is the default, 0.1 is a bot and 0.9 is a human).
<?php // Check if form was submitted:
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'SECRET_KEY';
$recaptcha_response = $_POST['recaptcha_response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
// Take action based on the score returned:
if (($recaptcha->score >= 0.5) && (isset($_POST["submit"]) || isset($_POST["submit_x"])))
{
//Insert your WA Universal Email Code block here
} else {
echo("Spam test failed");
}
} ?>
Call the JS in the head of your document, changing SITEKEY to your own Site Key:
<script src="https://www.google.com/recaptcha/api.js?render=SITEKEY"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('SITEKEY', { action: '' }).then(function (token) {
var recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
});
});
</script>
Add the following hidden field to your form:
<input type="hidden" name="recaptcha_response" id="recaptchaResponse">