ianhaney Posted June 15, 2020 Share Posted June 15, 2020 I am still fairly new to using phpmailer and it works but I have been asked to google recaptcha v2 checkbox into their contact form but unsure how to do that within the phpmailer code, below is what I currently have <?php // PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Base files require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; // create object of PHPMailer class with boolean parameter which sets/unsets exception. $mail = new PHPMailer(true); try { //$mail->isSMTP(); // using SMTP protocol $mail->Host = 'hostname'; // SMTP host as gmail $mail->SMTPAuth = true; // enable smtp authentication $mail->Username = 'emailaddress'; // sender gmail host $mail->Password = 'emailpassword'; // sender gmail host password $mail->SMTPSecure = 'ssl'; // for encrypted connection $mail->Port = 587; // port for SMTP $mail->setFrom('emailaddress', "Business Name"); // sender's email and name $mail->addAddress('emailaddress', "Business Name"); // receiver's email and name $mail->Subject = 'New Website Enquiry'; $mail->Body = "A new website enquiry has been made. The enquiry information is below" . "\r\n\r\n" . "Name: " . $_POST["name"] . "\r\n" . "Email: " . $_POST["email"] . "\r\n" . "Subject: " . $_POST["subjectline"] . "\r\n" . "Message: " . $_POST["message"]; $mail->send(); header('Location:enquiry-confirmation.php'); } catch (Exception $e) { // handle error. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } ?> Can anyone help please as to where the code needs to go to check if the checkbox has been checked or not Quote Link to comment Share on other sites More sharing options...
requinix Posted June 15, 2020 Share Posted June 15, 2020 It's not just a matter of a checkbox. How you use reCAPTCHA has nothing to do with PHPMailer or really anything else. Completely irrelevant. So what you need to do is look into how to incorporate reCAPTCHA into your script in general. Quote Link to comment Share on other sites More sharing options...
ianhaney Posted June 15, 2020 Author Share Posted June 15, 2020 I normally use this code: https://www.codexworld.com/new-google-recaptcha-with-php/ but that code does not send the email to the client for some reason, they receive their emails through office 365 so switched to phpmailer and they receive the emails now but they get bit of spam emails so need to prevent the spam emails from going through so need a idea of the code to prevent the contact form sending if the checkbox is not checked Do I take bits of code from the link I put and put inside the sendenquiry.php file I have which has the phpmailer code in I posted above? Quote Link to comment Share on other sites More sharing options...
ianhaney Posted June 15, 2020 Author Share Posted June 15, 2020 Think I just got it working with the following code <?php // PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Base files require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; // create object of PHPMailer class with boolean parameter which sets/unsets exception. $mail = new PHPMailer(true); try { //$mail->isSMTP(); // using SMTP protocol $mail->Host = 'host'; // SMTP host as gmail $mail->SMTPAuth = true; // enable smtp authentication $mail->Username = 'emailaddress'; // sender gmail host $mail->Password = 'password'; // sender gmail host password $mail->SMTPSecure = 'ssl'; // for encrypted connection $mail->Port = 587; // port for SMTP $mail->setFrom('emailaddress', "Business Name"); // sender's email and name $mail->addAddress('emailaddress', "Business Name"); // receiver's email and name $mail->Subject = 'New Website Enquiry'; $mail->Body = "A new website enquiry has been made. The enquiry information is below" . "\r\n\r\n" . "Name: " . $_POST["name"] . "\r\n" . "Email: " . $_POST["email"] . "\r\n" . "Subject: " . $_POST["subjectline"] . "\r\n" . "Message: " . $_POST["message"]; $response = $_POST["g-recaptcha-response"]; $url = 'https://www.google.com/recaptcha/api/siteverify'; $data = array( 'secret' => 'SECRETKEY', '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>"; echo "<br><a href=\"javascript:history.go(-1)\">Go Back To Form</a>"; } else if ($captcha_success->success==true) { $mail->send(); header('Location:enquiry-confirmation.php'); } } catch (Exception $e) { // handle error. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } ?> 1 Quote Link to comment Share on other sites More sharing options...
Strider64 Posted June 15, 2020 Share Posted June 15, 2020 (edited) I use SwiftMailer, but I don't bother to send the email and going through all the hassle of sending the email until I verify the user with Google's recaptcha. /* The Following to get response back from Google recaptcha */ $url = "https://www.google.com/recaptcha/api/siteverify"; $remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL); $response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer); $recaptcha_data = json_decode($response); /* The actual check of the recaptcha */ if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) { $success = "Mail was sent!"; $data['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); $data['phone'] = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $data['website'] = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $data['reason'] = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $data['comments'] = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $send = new Email($data); } else { $success = "You're not a human!"; // Not of a production server: } Edited June 15, 2020 by Strider64 unrelated code.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.