A form reply requires two things. An HTML form to gather user input as name/value pairs and a PHP script to receive the data and place it in an email and send it.
The HTML Form
Below is a simple form that gathers two name/value pairs, email and comments, and passes them to form_reply.php when the user clicks the Send button.
The name/value pairs are passed as an associative array called $HTTP_POST_VARS so we can use foreach to loop through the array and copy them into our own array called $formvars.
Gathering the Data
<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formvars[$varname] = $value;
?>
Cleaning the Input
Whenever you have any kind of form on your site that accepts input to be processed by a PHP script there is a rule that should never be broken.
Never trust user input. Never rely solely upon HTML size attributes or JavaScript event handlers to screen or limit user input.
These are easily circumvented since they run on the client side, where the mischief-makers have control, and not the server side where you have control.
All user input should be cleaned using PHP string functions to screen out dangerous characters and to limit size before being used by the rest of the script.
To clean the data we can create a function that will remove any characters that might be used to create spam, escape any mischievious shell commands and limit the size of the input.
We can also use the trim() function to remove leading and trailing whitespace and tab characters etc.
// Limit the size of each value to 1000 bytes
$size = 1000;
foreach($HTTP_POST_VARS as $varname => $value)
$formvars[$varname] = trim(clean($value, $size));
?>
The PHP mail() Function
The sendmail() function accepts five string arguments, three are required and two are optional.
The to argument specifies the receiver or receivers of the email (required).
The subject argument specifies the subject of the email (required).
The message argument specifies the message body of the email (required).
The headers argument specifies additional header such as CC (optional).
The parameters argument specifies additional parameters for the sendmail program (optional).
All that remains is to set up the arguments of the mail function then forward the user to a page that lets them know the email was sent.
Sending the email
<?php
// Set up e-mail address
$to = "info@somedomain.com";
// Setup the subject line
$subject = "Feedback from reply form:";
// Put together the body of the email
$message = "E-mail address: ".$formvars[email]."\r\n";
$message .= "Comments: ".$formvars[comments];
// Send the email
mail($to, $subject, $message);
// Forward the user to a thank you page
header("Location: thank_you.php");
?>
Note: You'll sometimes see a forwarding address being passed to a form script from a hidden field in an HTML form. This should be avoided because it can be changed to multiple addresses by a mischevious user to send spam.
Always try to hard code the forwarding address into your form script whenever possible.
// Limit the size of each value to 1000 bytes
$size = 1000;
foreach($HTTP_POST_VARS as $varname => $value)
$formvars[$varname] = trim(clean($value, $size));
// Set up e-mail address
$to = "info@somedomain.com";
// Setup the subject line
$subject = "Feedback from reply form:";
// Put together the body of the email
$message = "E-mail address: ".$formvars[email]."\r\n";
$message .= "Comments: ".$formvars[comments];
// Send the email
mail($to, $subject, $message);
// Forward the user to a thank you page
header("Location: thank_you.php");
?>