Results 1 to 2 of 2

Thread: PHP Contact Form

  1. #1
    Join Date
    Jun 2008
    Posts
    17

    PHP Contact Form

    I have a PHP form template that i use for all my clients websites. I'm not 100% confident with PHP but managed to put this together.

    One of my clients have asked if a function could be added where the form sends the user a copy of their enquiry to their email address. Im not too sure how to do that but i was wondering if anyone here knows how to do it?

    Here is my code:

    Code:
    <?php
    
    // process the email
    if (array_key_exists('send', $_POST)) {
      $to = 'test@test.com, $email'; // use your own email address
      $subject = 'Enquiry from MayFly Website';
      
      // list expected fields
      $expected = array('name', 'email', 'phone', 'website', 'enquiry');
      // set required fields
      $required = array('name', 'email', 'phone', 'enquiry');
      // create empty array for any missing fields
      $missing = array();
      
      // assume that there is nothing suspect
      $suspect = false;
      // create a pattern to locate suspect phrases
      $pattern = '/Content-Type:|Bcc:|Cc:/i';
      
      // function to check for suspect phrases
      function isSuspect($val, $pattern, &$suspect) {
        // if the variable is an array, loop through each element
    	// and pass it recursively back to the same function
    	if (is_array($val)) {
          foreach ($val as $item) {
    	    isSuspect($item, $pattern, $suspect);
    	    }
    	  }
        else {
          // if one of the suspect phrases is found, set Boolean to true
    	  if (preg_match($pattern, $val)) {
            $suspect = true;
    	    }
    	  }
        }
      
      // check the $_POST array and any sub-arrays for suspect content
      isSuspect($_POST, $pattern, $suspect);
      
      if ($suspect) {
        $mailSent = false;
    	unset($missing);
    	}
      else {
        // process the $_POST variables
        foreach ($_POST as $key => $value) {
          // assign to temporary variable and strip whitespace if not an array
          $temp = is_array($value) ? $value : trim($value);
    	  // if empty and required, add to $missing array
    	  if (empty($temp) && in_array($key, $required)) {
    	    array_push($missing, $key);
    	    }
    	  // otherwise, assign to a variable of the same name as $key
    	  elseif (in_array($key, $expected)) {
    	    ${$key} = $temp;
    	    }
    	  }
    	}
      
      // validate the email address
      if (!empty($email)) {
        // regex to ensure no illegal characters in email address 
    	$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    	// reject the email address if it doesn't match
    	if (!preg_match($checkEmail, $email)) {
    	  array_push($missing, 'email');
    	  }
    	}
      
      // go ahead only if not suspect and all required fields OK
      if (!$suspect && empty($missing)) {
        // build the message
        $message = "Name: $name\n\n";
    	$message .= "E-Mail: $email\n\n";
        $message .= "Telephone: $phone\n\n";
    	$message .= "Website: $website\n\n";
        $message .= "Enquiry: $enquiry";
    
        // limit line length to 70 characters
        $message = wordwrap($message, 85);
        
    	// create additional headers
    	$additionalHeaders = 'From: Mayfly Website Contact Form<test@test.co.uk>'; // PUT EMAIL ADDRESS HERE
    	if (!empty($email)) {
    	  $additionalHeaders .= "\r\nReply-To: $email";
    	  }
    	
        // send it  
        $mailSent = mail($to, $subject, $message, $additionalHeaders);
    	if ($mailSent) {
    	  // $missing is no longer needed if the email is sent, so unset it
    	  unset($missing);
    	  }
        }
      }
    ?>
    Cheers for any help

  2. #2
    Join Date
    Jan 2008
    Location
    Raleigh, NC
    Posts
    106
    By doing something like this:

    PHP Code:
    # $variable = "From: [email]me@mydomain.com[/email]"; 
    # $variable .= "\r\nCc: [email]he@$hisdomain.com[/email]" 
    that will allow the CC to be sent.

Similar Threads

  1. PHP Contact Form formatting in Outlook
    By shreddie08 in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 11 Jun 2007, 08:59 AM
  2. PHP contact form validation code
    By kade119 in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 26 Apr 2007, 03:50 AM
  3. PHP contact form script
    By femyram in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 30 Mar 2007, 01:58 PM
  4. Non PHP contact form
    By archvillain in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 20 Jan 2006, 05:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •