Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. <?php
  2.  
  3. // configure
  4. $from = 'Demo contact form <demo@domain.com>';
  5. $sendTo = 'Demo contact form <demo@domain.com>'; // Add Your Email
  6. $subject = 'New message from contact form';
  7. $fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
  8. $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
  9. $errorMessage = 'There was an error while submitting the form. Please try again later';
  10.  
  11. // let's do the sending
  12.  
  13. try
  14. {
  15. $emailText = "You have new message from contact form\n=============================\n";
  16.  
  17. foreach ($_POST as $key => $value) {
  18.  
  19. if (isset($fields[$key])) {
  20. $emailText .= "$fields[$key]: $value\n";
  21. }
  22. }
  23.  
  24. $headers = array('Content-Type: text/plain; charset="UTF-8";',
  25. 'From: ' . $from,
  26. 'Reply-To: ' . $from,
  27. 'Return-Path: ' . $from,
  28. );
  29.  
  30. mail($sendTo, $subject, $emailText, implode("\n", $headers));
  31.  
  32. $responseArray = array('type' => 'success', 'message' => $okMessage);
  33. }
  34. catch (\Exception $e)
  35. {
  36. $responseArray = array('type' => 'danger', 'message' => $errorMessage);
  37. }
  38.  
  39. if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  40. $encoded = json_encode($responseArray);
  41.  
  42. header('Content-Type: application/json');
  43.  
  44. echo $encoded;
  45. }
  46. else {
  47. echo $responseArray['message'];
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement