16.Sep.2008 at 16 | sam
A Simple PHP Contact Form
This is a simple php contact form / feedback form script I through together as an introduction to php for a friend.
Rather than write a tutorial I’ve heavily commented the code. Hope its useful to somebody.
_ -]*$/i';
if( !preg_match($testRegEx, $name) || !preg_match($testRegEx, $email) || !preg_match($testRegEx,$subject) ){
$errors[] = 'Your name, email address or subject contained invalid characters. Please re-enter.';
}
// if we have found any errors then we don't want to send the email
// we can tell if there are any errors by checking the number of entries in
// our $errors array.
// In php you check the size / number of items in an array using the count() function
if( count( $errors ) == 0){
// no errors, send the email using php's mail($to, $subject, $message, $headers) function
// first we create two more strings $full_subject and $full_message which
// will contain slightly modified versions of the original
// we prepend 'Contact form submission' to the user's subject, so when it shows up in our
// email client we know it came from our form
// Notice that php concatenates strings using the period . not a + like in java.
$full_subject = 'Contact form submission: ' . $subject;
$full_message = "$name sent the following message at " . date('H:i:s') . ' on ' . date('l jS F Y'). " from ip address {$_SERVER['REMOTE_ADDR']}\n\n";
$full_message .=$message;
// ok should probably explain what the two lines above do :0)
// "$name sent..." places the contents of the $name variable into the string
// date('H:i:s') gets the current time in the format 12:01:54 (hours:minutes:seconds)
// date('l jS F Y') gets the current date in the format "wednesday 16th July 2008"
// see www.php.net/date for more info (and note that you can access php info quickly at
// php.net by placing what you are looking for after the domain name, eg. php.net/arrays php.net/strings
// the {$_SERVER['REMOTE_ADDR']} imbeds the ip address of the current user into the string as well.
// $_SERVER is an array containing info from the server. array entries can be embedded in strings by
// surrounding them with curly brackets {...}
$full_email = "$name <$email>";
// the $headers parameter to the mail() command adds some important headers to the email, such as from and reply-to addresses.
$headers = "From: $full_email\r\nReply-To: $full_email\r\nErrors-To: $my_email";
// on your home pc, the mail function may not be able to work ok and will output an error.
if( mail($my_email, $full_subject, $full_message, $headers) ){
$submitted = true; // we use this variable later to decide whether to show the form or a thank-you message
}
else{ // if mail() failed then we have a server configuration error...
$errors[] = 'Sorry, due to server issues your message could not be sent. Please try talking to me instead :p';
}
}
}
/*
thats's most of the code, now we come out of php mode and add
an html page containing a simple form.
we mix a bit of php with it to check:
1) has the form been submitted? if so then just display a message, no form
2) if displaying the form, are there any errors? if so, display them
3) we output / echo / print any values the user has already submitted for the form
4) we echo the uri of this script into the form's action="" parameter so that the form
submits back to this page
A couple of notes about a couple of functions used below:
htmlspecialchars() takes a string and returns the same string with any characters that have special meaning in
html converted to display properly.
nl2br() Converts any newline characters in the string to html line break tags (because html just treats newlines as
spaces.
echo is a php function that prints the values of its parameters to the screen. You could also use print()
*/
?>
Thank You!
Thank you for contacting us. The following message was sent from , :
Subject:
Message:
Contact Us :)
Please correct the following errors and resubmit the form:
Feel free to send me some feedback on this if you found it useful :0)




This is exactly what I needed, and super-easy to understand. Thanks!