Advertisement
Guest User

php

a guest
Dec 14th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.99 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.                     $message .= $label.": " . htmlspecialchars($value, ENT_QUOTES) . "<br>\n";
  73.                 }
  74.             }
  75.  
  76.             $mail = new PHPMailer(true);
  77.  
  78.             try {
  79.  
  80.                 $mail->SMTPDebug = $debug;                            // Debug Mode
  81.  
  82.                 // Step 3 (Optional) - If you don't receive the email, try to configure the parameters below:
  83.  
  84.                 //$mail->IsSMTP();                                         // Set mailer to use SMTP
  85.                 //$mail->Host = 'mail.yourserver.com';                     // Specify main and backup server
  86.                 //$mail->SMTPAuth = true;                                  // Enable SMTP authentication
  87.                 //$mail->Username = 'user@example.com';                    // SMTP username
  88.                 //$mail->Password = 'secret';                              // SMTP password
  89.                 //$mail->SMTPSecure = 'tls';                               // Enable encryption, 'ssl' also accepted
  90.                 //$mail->Port = 587;                                       // TCP port to connect to
  91.  
  92.                 $mail->AddAddress($email);                                 // Add a recipient
  93.  
  94.                 //$mail->AddAddress('person2@domain.com', 'Person 2');     // Add another recipient
  95.                 //$mail->AddCC('person3@domain.com', 'Person 3');          // Add a "Cc" address.
  96.                 //$mail->AddBCC('person4@domain.com', 'Person 4');         // Add a "Bcc" address.
  97.  
  98.                 // From - Name
  99.                 $fromName = ( isset($_POST['name']) ) ? $_POST['name'] : 'Website User';
  100.                 $mail->SetFrom($email, $fromName);
  101.  
  102.                 // Repply To
  103.                 if( isset($_POST['email']) ) {
  104.                     $mail->AddReplyTo($_POST['email'], $fromName);
  105.                 }
  106.  
  107.                 $mail->IsHTML(true);                                        // Set email format to HTML
  108.  
  109.                 $mail->CharSet = 'UTF-8';
  110.  
  111.                 $mail->Subject = $subject;
  112.                 $mail->Body    = $message;
  113.  
  114.                 // Step 4 - If you don't want to attach any files, remove that code below
  115.                 if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
  116.                     $mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
  117.                 }
  118.  
  119.                 $mail->Send();
  120.  
  121.                 $arrResult = array ('response'=>'success');
  122.  
  123.             } catch (Exception $e) {
  124.                 $arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
  125.             } catch (\Exception $e) {
  126.                 $arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
  127.             }
  128.  
  129.         } else {
  130.             $arrResult = array ('response'=>'error','errorMessage'=>'Robot verification failed, please try again');
  131.         }
  132.  
  133.     } else {
  134.         $arrResult = array ('response'=>'error','errorMessage'=>'Please click on the reCAPTCHA box.');
  135.     }
  136. }
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement