The following PHP script allows a web site owner to collect information via a web form and have that information be submitted to an email address.
- The web site must reside on www.gmu.edu or some other web server that has PHP enabled and the capability to send mail. This will NOT work on mason.gmu.edu.
- The file must have a
How to Set Up an Email Form
Create a new file. This can be either an empty file or a page using your existing web template.
Copy the code below into the file.
- If you’re using Dreamweaver, be sure to do this in Code View.
- If you are inserting the code into an existing template, make sure you put it in a place on the page where it makes sense for a Form Sent! message to appear.
Edit the first few lines appropriately:
Save the file.
Create your web form. The form’s method should be POST (as opposed to GET), and the action should point to the file you just made. Each of the form fields should have a unique name that properly describes the field.
Upload the files to the server. To make sure the script is working do a test submit. If the form data arrives in the intended recipient’s email box, the script is working.
EMAIL FORM by Josh Hughes (firstname.lastname@example.org)
Free to use and adjust as needed
/* Change these values as needed ($to determines who receives the form data) */
$to = "email@example.com";
$from = "firstname.lastname@example.org";
$subject = "Email Form";
/* If you'd like to make sure certain fields have been filled out,
enter a comma-delimited list of required field names.
Example: $requiredFields = "field_name1, field_name2"; */
$requiredFields = "";
/* ------- ADVANCED EDITING BELOW THIS LINE ------- */
/* Make sure a form was submitted */
$missingFields = true;
$missingFields = false;
/* Check required fields */
if ((trim($requiredFields) != '') and ($missingFields != true))
$checkFields = explode(',', $requiredFields);
for ($i = 0; $i < count($checkFields); $i++)
if (trim($_POST[trim($checkFields[$i])]) == '')
$missingFields = true;
/* If there are missing fields, print an error */
print "<h1>Missing Fields</h1>
<p>Please go back and fill out all of the required fields.</p>";
$from = sprintf("From: %s", $from);
$message = "Submitted Form Values:\n\n";
/* The message is a list in the following format:
Field_name: Value of the Field */
foreach($_POST as $key => $value)
$message .= sprintf("%s: %s\n\n", $key, htmlentities($value));
/* Send the message */
$success = mail($to, $subject, $message, $from);
/* Print a success or error message */
print "<h1>Form Sent!</h1>
<p>Thank you for your submission!</p>";
<p>The form was <strong>NOT</strong> sent! There seems to be some sort of malfunction.</p>";