|Article Time Stamp: 14 March 2006, 10:36:14 GMT+7|
How to create PHP Mail Form
Step 1: Create your form
Here's the simple script:
Save this file as form.php.
You'll notice I used process.php as my form action. This is because the form data is going to be submitted to that file for actual mailing. I also wanted to limit how much data could be submitted. This is for security reasons.
Step 2: Processing the data
Now create a file called process.php.
First you want to get all the variables that were just sent to the page. Using the extract function, we can create identically named variables out of the $_POST array. In English this means that all the variables that have come to this page via the POST method (see the form above) will now be made into nice single-word variables.
Now we want to make the data friendly for us.
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);
Now we can do our mailing.
mail('firstname.lastname@example.org',$subject,$text,"From: $name <$email>");
And finally redirect the user back to the form:
So our code for process.php looks like this:
There's plenty more you can do with mail().
see http://id2.php.net/mail for complete mail() function
But, before you use this method, I'm very recommended that you read another topic "Security Hole Mail Header Injection at PHP" to prevent someone use your mail form to do spamming or something nasty to your server