Guest User

contact-form

a guest
Aug 10th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. /*
  3. Name:           Contact Form
  4. Written by:     Okler Themes - (http://www.okler.net)
  5. Theme Version:  8.0.0
  6. */
  7.  
  8. namespace PortoContactForm;
  9.  
  10. session_cache_limiter('nocache');
  11. header('Expires: ' . gmdate('r', 0));
  12.  
  13. header('Content-type: application/json');
  14.  
  15. use PHPMailer\PHPMailer\PHPMailer;
  16. use PHPMailer\PHPMailer\Exception;
  17.  
  18. require 'php-mailer/src/PHPMailer.php';
  19. require 'php-mailer/src/SMTP.php';
  20. require 'php-mailer/src/Exception.php';
  21.  
  22. // Step 1 - Enter your email address below.
  23. $email = '[email protected]';
  24.  
  25. // If the e-mail is not working, change the debug option to 2 | $debug = 2;
  26. $debug = 2;
  27.  
  28. // If contact form don't has the subject input change the value of subject here
  29. $subject = ( isset($_POST['subject']) ) ? $_POST['subject'] : 'Define subject in php/contact-form.php line 29';
  30.  
  31. $message = '';
  32.  
  33. foreach($_POST as $label => $value) {
  34.     $label = ucwords($label);
  35.  
  36.     // Use the commented code below to change label texts. On this example will change "Email" to "Email Address"
  37.  
  38.     // if( $label == 'Email' ) {              
  39.     //  $label = 'Email Address';              
  40.     // }
  41.  
  42.     // Checkboxes
  43.     if( is_array($value) ) {
  44.         // Store new value
  45.         $value = implode(', ', $value);
  46.     }
  47.  
  48.     $message .= $label.": " . htmlspecialchars($value, ENT_QUOTES) . "<br>\n";
  49. }
  50.  
  51. $mail = new PHPMailer(true);
  52.  
  53. try {
  54.  
  55.     $mail->SMTPDebug = $debug;                                 // Debug Mode
  56.  
  57.     // Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
  58.  
  59.     //$mail->IsSMTP();                                         // Set mailer to use SMTP
  60.     //$mail->Host = 'mail.yourserver.com';                     // Specify main and backup server
  61.     //$mail->SMTPAuth = true;                                  // Enable SMTP authentication
  62.     //$mail->Username = '[email protected]';                    // SMTP username
  63.     //$mail->Password = 'secret';                              // SMTP password
  64.     //$mail->SMTPSecure = 'tls';                               // Enable encryption, 'ssl' also accepted
  65.     //$mail->Port = 587;                                       // TCP port to connect to
  66.  
  67.     $mail->AddAddress($email);                                 // Add another recipient
  68.  
  69.     //$mail->AddAddress('[email protected]', 'Person 2');     // Add a secondary recipient
  70.     //$mail->AddCC('[email protected]', 'Person 3');          // Add a "Cc" address.
  71.     //$mail->AddBCC('[email protected]', 'Person 4');         // Add a "Bcc" address.
  72.  
  73.     // From - Name
  74.     $fromName = ( isset($_POST['name']) ) ? $_POST['name'] : 'Website User';
  75.     $mail->SetFrom($email, $fromName);
  76.  
  77.     // Repply To
  78.     if( isset($_POST['email']) ) {
  79.         $mail->AddReplyTo($_POST['email'], $fromName);
  80.     }
  81.  
  82.     $mail->IsHTML(true);                                       // Set email format to HTML
  83.  
  84.     $mail->CharSet = 'UTF-8';
  85.  
  86.     $mail->Subject = $subject;
  87.     $mail->Body    = $message;
  88.  
  89.     $mail->Send();
  90.     $arrResult = array ('response'=>'success');
  91.  
  92. } catch (Exception $e) {
  93.     $arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
  94. } catch (\Exception $e) {
  95.     $arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
  96. }
  97.  
  98. if ($debug == 0) {
  99.     echo json_encode($arrResult);
  100. }
Add Comment
Please, Sign In to add comment