PDA

View Full Version : PHP Contact Form



k_wills
15 Oct 2008, 09:48 AM
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:



<?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

rezzy
15 Oct 2008, 12:10 PM
By doing something like this:


# $variable = "From: me@mydomain.com";
# $variable .= "\r\nCc: he@$hisdomain.com"

that will allow the CC to be sent.