Advertisement
Guest User

Untitled

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