Advertisement
Guest User

contact-form.php

a guest
Apr 20th, 2017
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Name:           Demo Form
  4. */
  5. ini_set('allow_url_fopen', true);
  6. session_cache_limiter('nocache');
  7. header('Expires: ' . gmdate('r', 0));
  8.  
  9. header('Content-type: application/json');
  10.  
  11. if(isset($_POST['recaptcha']) && !empty($_POST['recaptcha']))
  12. {
  13.     //your site secret key
  14.     $secret = '6Lfs1R0UAAAAABBa_bGcxwtnwmhASeY3ia5GltNJ';
  15.    
  16.     if( function_exists('curl_version') ) {
  17.         // reCAPTCHA - Using CURL
  18.         $fields = array(
  19.             'secret'    =>  $secret,
  20.             'response'  =>  $_POST['recaptcha'],
  21.             'remoteip'  =>  $_SERVER['REMOTE_ADDR']
  22.         );
  23.  
  24.         $verifyResponse = curl_init("https://www.google.com/recaptcha/api/siteverify");
  25.         curl_setopt($verifyResponse, CURLOPT_RETURNTRANSFER, true);
  26.         curl_setopt($verifyResponse, CURLOPT_TIMEOUT, 15);
  27.         curl_setopt($verifyResponse, CURLOPT_POSTFIELDS, http_build_query($fields));
  28.         $responseData = json_decode(curl_exec($verifyResponse));
  29.         curl_close($verifyResponse);
  30.     } else {
  31.         $arrResult = array ('response'=>'error','errorMessage'=>'You need CURL or file_get_contents() activated in your server. Please contact your host to activate.');
  32.         echo json_encode($arrResult);
  33.         die();
  34.     }
  35.     if($responseData->success) {
  36.  
  37.         require_once('php-mailer/PHPMailerAutoload.php');
  38.         // Step 1 - Enter your email address below.
  39.         $email = 'adortok@ndacorp.com';
  40.  
  41.         // If the e-mail is not working, change the debug option to 2 | $debug = 2;
  42.         $debug = 0;
  43.  
  44.         $fields = array(
  45.             0 => array(
  46.                 'text' => 'Name',
  47.                 'val' => $_POST['name']
  48.             ),
  49.             1 => array(
  50.                 'text' => 'Email',
  51.                 'val' => $_POST['email']
  52.             ),
  53.             2 => array(
  54.                 'text' => 'Company',
  55.                 'val' => $_POST['company']
  56.             ),
  57.             3 => array(
  58.                 'text' => 'Phone',
  59.                 'val' => $_POST['phone']
  60.             ),
  61.             4 => array(
  62.                 'text' => 'Region',
  63.                 'val' => $_POST['region']
  64.             ),
  65.             5 => array(
  66.                 'text' => 'Checkboxes',
  67.                 'val' => implode($_POST['checkboxes'], ", ")
  68.             ),
  69.             6 => array(
  70.                 'text' => 'Additional Info',
  71.                 'val' => $_POST['comment']
  72.             )
  73.         );
  74.  
  75.         $message = '';
  76.  
  77.         foreach($fields as $field) {
  78.             $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
  79.         }
  80.  
  81.         $mail = new PHPMailer(true);
  82.  
  83.         try {
  84.  
  85.             $mail->SMTPDebug = $debug;                                 // Debug Mode
  86.  
  87.             // Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
  88.  
  89.             $mail->IsSMTP();                                         // Set mailer to use SMTP
  90.             $mail->Host = '10.10.1.32';                    // Specify main and backup server
  91.             //$mail->SMTPAuth = true;                                  // Enable SMTP authentication
  92.             //$mail->Username = 'user@example.com';                    // SMTP username
  93.             //$mail->Password = 'secret';                              // SMTP password
  94.             //$mail->SMTPSecure = 'tls';                               // Enable encryption, 'ssl' also accepted
  95.             //$mail->Port = 587;                                       // TCP port to connect to
  96.  
  97.             $mail->AddAddress($email);                                 // Add another recipient
  98.  
  99.             //$mail->AddAddress('person2@domain.com', 'Person 2');     // Add a secondary recipient
  100.             //$mail->AddCC('person3@domain.com', 'Person 3');          // Add a "Cc" address.
  101.             //$mail->AddBCC('person4@domain.com', 'Person 4');         // Add a "Bcc" address.
  102.  
  103.             $mail->SetFrom('nda@ndacorp.com', $_POST['name']);
  104.             $mail->AddReplyTo($_POST['email'], $_POST['name']);
  105.  
  106.             $mail->IsHTML(true);                                  // Set email format to HTML
  107.  
  108.             $mail->CharSet = 'UTF-8';
  109.  
  110.             $mail->Subject = 'Demo Request Submitted';
  111.             $mail->Body    = $message;
  112.  
  113.             $mail->Send();
  114.             $arrResult = array ('response'=>'success');
  115.  
  116.         } catch (phpmailerException $e) {
  117.             $arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
  118.         } catch (Exception $e) {
  119.             $arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
  120.         }
  121.  
  122.         if ($debug == 0) {
  123.             echo json_encode($arrResult);
  124.         }
  125.     } else {
  126.         $arrResult = array ('response'=>'error','errorMessage'=>'Robot verification failed, please try again');
  127.         echo json_encode($arrResult);
  128.     }
  129.  
  130. } else {
  131.     $arrResult = array ('response'=>'error','errorMessage'=>'Please click on the reCAPTCHA box.');
  132.     echo json_encode($arrResult);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement