$secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
);
$verifyResponse = curl_init("https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verifyResponse, CURLOPT_RETURNTRANSFER, true);
curl_setopt($verifyResponse, CURLOPT_TIMEOUT, 15);
curl_setopt($verifyResponse, CURLOPT_POSTFIELDS, http_build_query($fields));
$responseData = json_decode(curl_exec($verifyResponse));
curl_close($verifyResponse);
} else {
$arrResult = array ('response'=>'error','errorMessage'=>'You need CURL or file_get_contents() activated in your server. Please contact your host to activate.');
echo json_encode($arrResult);
die();
}
if($responseData->success) {
$message = '';
foreach($_POST as $label => $value) {
if( $label != 'g-recaptcha-response' ) {
$label = ucwords($label);
// Use the commented code below to change label texts. On this example will change "Email" to "Email Address"
// if( $label == 'Email' ) {
// $label = 'Email Address';
// }
// Checkboxes
if( is_array($value) ) {
// Store new value
$value = implode(', ', $value);
}
$message .= $label.": " . htmlspecialchars($value, ENT_QUOTES) . "
\n";
}
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug; // Debug Mode
// Step 3 (Optional) - If you don't receive the email, try to configure the parameters below:
//$mail->IsSMTP(); // Set mailer to use SMTP
//$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'user@example.com'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->AddAddress($email); // Add a recipient
//$mail->AddAddress('person2@domain.com', 'Person 2'); // Add another recipient
//$mail->AddCC('person3@domain.com', 'Person 3'); // Add a "Cc" address.
//$mail->AddBCC('person4@domain.com', 'Person 4'); // Add a "Bcc" address.
// From - Name
$fromName = ( isset($_POST['name']) ) ? $_POST['name'] : 'Website User';
$mail->SetFrom($fromemail, $fromName);
// Repply To
if( isset($_POST['email']) ) {
$mail->AddReplyTo($_POST['email'], $fromName);
}
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
// Step 4 - If you don't want to attach any files, remove that code below
if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
}
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (\Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
} else {
$arrResult = array ('response'=>'error','errorMessage'=>'Robot verification failed, please try again');
}
} else {
$arrResult = array ('response'=>'error','errorMessage'=>'Please click on the reCAPTCHA box.');
}
}
?>