Advertisement
Guest User

Untitled

a guest
Jan 10th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <?php
  2. /*
  3. Name: Contact Form - Google Recaptcha v3
  4. Written by: Okler Themes - (http://www.okler.net)
  5. Theme Version: 9.9.0
  6. */
  7.  
  8. namespace PortoContactForm;
  9.  
  10. ini_set('allow_url_fopen', true);
  11.  
  12. session_cache_limiter('nocache');
  13. header('Expires: ' . gmdate('r', 0));
  14.  
  15. header('Content-type: application/json');
  16.  
  17. use PHPMailer\PHPMailer\PHPMailer;
  18. use PHPMailer\PHPMailer\Exception;
  19.  
  20. require 'php-mailer/src/PHPMailer.php';
  21. require 'php-mailer/src/SMTP.php';
  22. require 'php-mailer/src/Exception.php';
  23.  
  24. if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
  25.  
  26. // Your Google reCAPTCHA generated Secret Key here
  27. $secret = '';
  28.  
  29. if( ini_get('allow_url_fopen') ) {
  30. //reCAPTCHA - Using file_get_contents()
  31. $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
  32. $responseData = json_decode($verifyResponse);
  33. } else if( function_exists('curl_version') ) {
  34. // reCAPTCHA - Using CURL
  35. $fields = array(
  36. 'secret' => $secret,
  37. 'response' => $_POST['g-recaptcha-response'],
  38. 'remoteip' => $_SERVER['REMOTE_ADDR']
  39. );
  40.  
  41. $verifyResponse = curl_init("https://www.google.com/recaptcha/api/siteverify");
  42. curl_setopt($verifyResponse, CURLOPT_RETURNTRANSFER, true);
  43. curl_setopt($verifyResponse, CURLOPT_TIMEOUT, 15);
  44. curl_setopt($verifyResponse, CURLOPT_POSTFIELDS, http_build_query($fields));
  45. $responseData = json_decode(curl_exec($verifyResponse));
  46. curl_close($verifyResponse);
  47. } else {
  48. $arrResult = array ('response'=>'error','errorMessage'=>'You need CURL or file_get_contents() activated in your server. Please contact your host to activate.');
  49. echo json_encode($arrResult);
  50. die();
  51. }
  52.  
  53. if($responseData->success) {
  54.  
  55. // Step 1 - Enter your email address below.
  56. $email = '[email protected]';
  57.  
  58. // If the e-mail is not working, change the debug option to 2 | $debug = 2;
  59. $debug = 0;
  60.  
  61. // If contact form don't have the subject input, change the value of subject here
  62. $subject = ( isset($_POST['subject']) ) ? $_POST['subject'] : 'Define subject in php/contact-form-recaptcha.php line 62';
  63.  
  64. $message = '';
  65.  
  66. foreach($_POST as $label => $value) {
  67. if( $label != 'g-recaptcha-response' ) {
  68. $label = ucwords($label);
  69.  
  70. // Use the commented code below to change label texts. On this example will change "Email" to "Email Address"
  71.  
  72. // if( $label == 'Email' ) {
  73. // $label = 'Email Address';
  74. // }
  75.  
  76. // Checkboxes
  77. if( is_array($value) ) {
  78. // Store new value
  79. $value = implode(', ', $value);
  80. }
  81.  
  82. $message .= $label.": " . nl2br(htmlspecialchars($value, ENT_QUOTES)) . "<br>";
  83. }
  84. }
  85.  
  86. $mail = new PHPMailer(true);
  87.  
  88. try {
  89.  
  90. $mail->SMTPDebug = $debug; // Debug Mode
  91.  
  92. // Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
  93.  
  94. //$mail->IsSMTP(); // Set mailer to use SMTP
  95. //$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
  96. //$mail->SMTPAuth = true; // Enable SMTP authentication
  97. //$mail->Username = '[email protected]'; // SMTP username
  98. //$mail->Password = 'secret'; // SMTP password
  99. //$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
  100. //$mail->Port = 587; // TCP port to connect to
  101.  
  102. $mail->AddAddress($email); // Add another recipient
  103.  
  104. //$mail->AddAddress('[email protected]', 'Person 2'); // Add a secondary recipient
  105. //$mail->AddCC('[email protected]', 'Person 3'); // Add a "Cc" address.
  106. //$mail->AddBCC('[email protected]', 'Person 4'); // Add a "Bcc" address.
  107.  
  108. // From - Name
  109. $fromName = ( isset($_POST['name']) ) ? $_POST['name'] : 'Website User';
  110. $mail->SetFrom($email, $fromName);
  111.  
  112. // Reply To
  113. if( isset($_POST['email']) && !empty($_POST['email']) ) {
  114. $mail->AddReplyTo($_POST['email'], $fromName);
  115. }
  116.  
  117. $mail->IsHTML(true); // Set email format to HTML
  118.  
  119. $mail->CharSet = 'UTF-8';
  120.  
  121. $mail->Subject = $subject;
  122. $mail->Body = $message;
  123.  
  124. // Step 3 - If you don't want to attach any files, remove that code below
  125. if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
  126. $mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
  127. }
  128.  
  129. $mail->Send();
  130. $arrResult = array ('response'=>'success');
  131.  
  132. } catch (Exception $e) {
  133. $arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
  134. } catch (\Exception $e) {
  135. $arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
  136. }
  137.  
  138. if ($debug == 0) {
  139. echo json_encode($arrResult);
  140. }
  141.  
  142. } else {
  143. $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Verifcation failed (no success). Please contact the website administrator.');
  144. echo json_encode($arrResult);
  145. }
  146.  
  147. } else {
  148. $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Invalid token. Please contact the website administrator.');
  149. echo json_encode($arrResult);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement