Advertisement
ujiajah1

send_form_email.php

May 13th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['email'])) {
  3.  
  4.     // EDIT THE 2 LINES BELOW AS REQUIRED
  5.     $email_to = "pujiermanto@gmail.com";
  6.     $email_subject = "Contact me/us";
  7.  
  8.     function died($error) {
  9.         // your error code can go here
  10.         echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  11.         echo "These errors appear below.<br /><br />";
  12.         echo $error."<br /><br />";
  13.         echo "Please go back and fix these errors.<br /><br />";
  14.         die();
  15.     }
  16.  
  17.     // validation expected data exists
  18.     if(!isset($_POST['first_name']) ||
  19.         !isset($_POST['last_name']) ||
  20.         !isset($_POST['email']) ||        
  21.         !isset($_POST['comments'])) {
  22.         died('We are sorry, but there appears to be a problem with the form you submitted.');      
  23.     }
  24.  
  25.     $first_name = $_POST['first_name']; // required
  26.     $last_name = $_POST['last_name']; // required
  27.     $email_from = $_POST['email']; // required    
  28.     $comments = $_POST['comments']; // required
  29.  
  30.     $error_message = "";
  31.     $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  32.   if(!preg_match($email_exp,$email_from)) {
  33.     $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  34.   }
  35.     $string_exp = "/^[A-Za-z\s.'-]+$/";
  36.   if(!preg_match($string_exp,$first_name)) {
  37.     $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  38.   }
  39.   if(!preg_match($string_exp,$last_name)) {
  40.     $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  41.   }
  42.   if(strlen($comments) < 2) {
  43.     $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  44.   }
  45.   if(strlen($error_message) > 0) {
  46.     died($error_message);
  47.   }
  48.     $email_message = "Form details below.\n\n";
  49.  
  50.     function clean_string($string) {
  51.       $bad = array("content-type","bcc:","to:","cc:","href");
  52.       return str_replace($bad,"",$string);
  53.     }
  54.  
  55.     $email_message .= "First Name: ".clean_string($first_name)."\n";
  56.     $email_message .= "Last Name: ".clean_string($last_name)."\n";
  57.     $email_message .= "Email: ".clean_string($email_from)."\n";  
  58.     $email_message .= "Comments: ".clean_string($comments)."\n";
  59.  
  60. // create email headers
  61. $headers = 'From: '.$email_from."\r\n".
  62. 'Reply-To: '.$email_from."\r\n" .
  63. 'X-Mailer: PHP/' . phpversion();
  64. @mail($email_to, $email_subject, $email_message, $headers);
  65. sleep(2);
  66. echo "<meta http-equiv='refresh' content=\"0; url=http://localhost/pujiermanto/index.php\">";
  67. ?>
  68.  
  69. <?php
  70. }
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement