Am not really a coder, but understand a little... Bought a web template with a contact form. It sends the form to a php for validation, which in turn sends it to me in an email. I can get the Recaptcha to appear fine on the form... But the php seems to fail (gives me a 500 error, but it DOES send out the form email to me)... Sorry... I'm tearing my hair out!
The following is the php file:
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => 'secret_code_here',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
} else if ($captcha_success->success==true) {
use VanillaForm\MailSender;
use VanillaForm\Utils;
require_once('inc/Utils.php');
require_once('inc/MailSender.php');
header('Access-Control-Allow-Origin: *');
/*
* Check if call is a POST request (data was sent by form).
* While it's not a POST request it returns OK, which can be handy with checking that the script is alive.
*/
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
if (function_exists('mail')) {
die('OK');
} else {
die('PHP parser works, but <b>mail()</b> function seems to doesn\'t exist');
}
}
/*
*
* Vanilla Configuration starts HERE
*
*/
$vf_config = array(
/*** Code Snippet - quick-start ***/
/**
* Recipient's e-mail. To this e-mail email will be sent.
* E.g. Single recipient
* 'emailRecipients' => '[email protected]',
*
* E.g. Multiple recipients
* 'emailRecipients' => '[email protected], [email protected]',
*/
'emailRecipients' => '[email protected]',
/**
* If is not empty it sets a header From in e-mail message (sets sender e-mail).
* Note: some hosting servers can block sending e-mails with custom From field in header.
* If so, leave this field as empty.
* E.g. Single recipient
* 'emailSender' => '[email protected]',
*/
'emailSender' => '[email protected]'
/*** /Code Snippet - quick-start ***/
);
$vfSender = new MailSender($vf_config);
/*
* Some variable may need translation for better readability.
* You can access and modify variables by:
* $vfSender->data['variable-name']
*/
switch ($vfSender->data['gender']) {
case "F":
$vfSender->data['gender'] = "Female";
break;
case "M":
$vfSender->data['gender'] = "Male";
break;
default:
$vfSender->data['gender'] = "Not selected";
break;
}
// Define here subject of the e-mail message
$subject = 'Website Contact Form - New Message from '.$vfSender->data['name'];
// Define here content of the e-mail message
$content = "Hey,
You've received new message from your website. Check the details below:
Sender's IP address: ".Utils::getIp()."
Name: {NAME}
E-mail: {EMAIL}
Phone number: {TEL}
Subject: {SUBJECT}
Message:
{MESSAGE}
";
// Set subject and message content
$vfSender->setMessage($subject, $content);
/*
* Vanilla Configuration ends HERE
*/
die($vfSender->sendMessage());
}