Sinambela

contact.php

Apr 12th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. <?php
  2. /*
  3. * Contact Form Class
  4. */
  5. header('Cache-Control: no-cache, must-revalidate');
  6. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  7. header('Content-type: application/json');
  8. $admin_email = '[email protected]'; // Your Email
  9. $message_min_length = 5; // Min Message Length
  10. class Contact_Form{
  11. function __construct($details, $email_admin, $message_min_length){
  12.  
  13. $this->name = stripslashes($details['name']);
  14. $this->email = trim($details['email']);
  15. $this->subject = 'Contact from Your Website'; // Subject
  16. $this->message = stripslashes($details['message']);
  17.  
  18. $this->email_admin = $email_admin;
  19. $this->message_min_length = $message_min_length;
  20.  
  21. $this->response_status = 1;
  22. $this->response_html = '';
  23. }
  24. private function validateEmail(){
  25. $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
  26.  
  27. if($this->email == '') {
  28. return false;
  29. } else {
  30. $string = preg_replace($regex, '', $this->email);
  31. }
  32.  
  33. return empty($string) ? true : false;
  34. }
  35. private function validateFields(){
  36. // Check name
  37. if(!$this->name)
  38. {
  39. $this->response_html .= '<p>Please enter your name</p>';
  40. $this->response_status = 0;
  41. }
  42. // Check email
  43. if(!$this->email)
  44. {
  45. $this->response_html .= '<p>Please enter an e-mail address</p>';
  46. $this->response_status = 0;
  47. }
  48.  
  49. // Check valid email
  50. if($this->email && !$this->validateEmail())
  51. {
  52. $this->response_html .= '<p>Please enter a valid e-mail address</p>';
  53. $this->response_status = 0;
  54. }
  55.  
  56. // Check message length
  57. if(!$this->message || strlen($this->message) < $this->message_min_length)
  58. {
  59. $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
  60. $this->response_status = 0;
  61. }
  62. }
  63. private function sendEmail(){
  64. $mail = mail($this->email_admin, $this->subject, $this->message,
  65. "From: ".$this->name." <".$this->email.">\r\n"
  66. ."Reply-To: ".$this->email."\r\n"
  67. ."X-Mailer: PHP/" . phpversion());
  68.  
  69. if($mail)
  70. {
  71. $this->response_status = 1;
  72. $this->response_html = '<p>Thank You!</p>';
  73. }
  74. }
  75. function sendRequest(){
  76. $this->validateFields();
  77. if($this->response_status)
  78. {
  79. $this->sendEmail();
  80. }
  81. $response = array();
  82. $response['status'] = $this->response_status;
  83. $response['html'] = $this->response_html;
  84.  
  85. echo json_encode($response);
  86. }
  87. }
  88. $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
  89. $contact_form->sendRequest();
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment