PHP Form Mail Script

I’ve had some requests to show you the php script I used to process and send the email from the HTML W3C Tutorial. So here you are. I’ll go through everything, like before, by including the code in black and white and my descriptions of the code in a blue box.

Code is here in a zipped archive.

<html>

You begin a PHP script with the HTML tag, just like normal HTML. You would save the file with the extention .PHP instead of .html though.

<head><title>PHP Mail Sender</title></head>

Here is the definition of the header section using HTML tags. I trust you know what these means. If not view the HTML W3C tutorial here.

<body>

Here is the body tag that tells the browser to display the content that follows.

<?php

This tag tells the browser, that it is going to be reading PHP code, instead of normal HTML.

/* This program will be sent the data from the HTML file formtest2.htm.
It will make sure the required variables have a value set.
If not it will return an error message.
It will create variables and assign the data sent to the correct variables.
It will print on the visitors screen those values sent.
It will assign all of the data sent to a variable named message.
Then email the message to the chosen email address.
If any errors occured, it will make the visitor aware of how to correct the errors. */

This is a PHP comment section, that will list everything this program will try to accomplish. You begin a comment with the code /* and end it with the code */

/* Check if the form was submitted */
if (isset($_POST['submitted'])) {
$errors = array(); // Create an Error Array

Here is another comment. Now I’ll describe the second line. In the html file that sent data to this program, there was a line of html that looked like this:
<input type=”hidden” name=”submitted” value=”TRUE” />
This line of HTML code, creates a variable named submitted and assigns the value of TRUE to that variable. Then that variable and value are sent to this application for processing. The line above is asking, “if the variable submitted is set?” You are able to access the variable submitted’s value by surrounding its name with $_POST[' and ']. If the value is equal to TRUE, which you can see that it is in the html code above, the program should process every statement that lies between the opening bracket { and }.

Then the code: $errors = array(); creates an array that it will store any errors, that occur in. An array is like a box that you can put information in. This line of code says that, “a box should be created and it should be assigned the variable name errors. The code: // Create an Error Array, is another way to comment in PHP. If you type // in PHP code, all information till the end of the line will be ignored. And finally, you end all code statements with a semicolon ( ; ).

/* Check to see if email, subject and message variables have a value */
if (empty($_POST['email'])) {
$errors[] = ‘Please Enter an Email.’;

}

if (empty($_POST['subject'])) {
$errors[] = ‘Please Enter a Subject.’;
}

if (empty($_POST['message'])) {
$errors[] = ‘Please Enter a Message.’;
}

These 3 blocks of code perform the same action on 3 different variables, that were sent by the HTML file. The code asks, “Is the variable, that was sent, have a value assigned to it? If it doesn’t put an error message in the error array, or error box. If their is a value assigned to the variable, do nothing.” The code performs this check on the variables email, subject and message.

/* Check to see if any required data was not entered. If an error occured, report that to the visitor */
if (empty($errors)) {

Here we have another comment. Then the code asks, “Does the error array have any errors in it? If not proceed reading the code that lies between the { and } symbols. If their are errors in the error array, skip following the code between the { and } symbols. Then continue reading the code immediately after the symbol }.

/* All form fields are automatically passed to the PHP script through the array $_POST. Then they are assigned to new PHP variables.*/
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$textinput = $_POST['textinput'];
$password = $_POST['password'];
$radioinput = $_POST['radioinput'];
$checkboxinput = $_POST['checkboxinput'];
$selectinput = $_POST['selectinput'];
$optioninput = $_POST['optioninput'];

Here we are taking all of the information that was past from the HTML W3C file and assigning them to a new variable.

/* Print out the information that the user inputted */
echo ” Email: $email <br />”;
echo ” Subject: $subject <br />”;
echo ” Message: $message <br />”;
echo ” Text Input: $textinput <br />”;
echo ” Password Input: $password <br />”;
echo ” Radio Input: $radioinput <br />”;
echo ” Checkbox Input: $checkboxinput <br />”;
echo ” Select Input: $selectinput <br />”;
echo ” Option Input: $optioninput <br />”;

Here we are printing out all of the variables and the variable values. The word echo will send whatever is between the quotes, to the visitor’s browser screen.

/* Assign all of the visitor inputted data to the variable named message */
$message = ‘Text Area: ‘ . $message . ‘ Text Input: ‘ . $textinput . ‘ Password Input: ‘ . $password . ‘ Radio Input: ‘ . $radioinput . ‘ Checkbox Input: ‘ . $checkboxinput . ‘ Select Input: ‘ . $selectinput . ‘ Option Input: ‘ . $optioninput;

Here we are combining strings of text, enclosed with a quote, to variable values. They are combined by typing a period between those strings and variables we want combined. After they are combined into one string, we assign that value to the variable message.

/* Sends the mail and outputs the “Thank you” string if the mail is successfully sent, or the error string otherwise. */
mail($email,$subject,$message);

echo “Message Sent, Thank you <br />”;

First we have a comment. Then we use the mail function to send an email to the email assigned to the variable $email. The subject of the email will be what ever the value of subject was and the message in the email, will be the value assigned to the variable message.

Then we use the echo function to display the words, “Message Sent, Thank you <br />”, on the visitors browser screen.

/* Creates a link the visitor can click to go back to the original page */
echo “<a href=’javascript:history.back(1);’>Click Here to go Back to the Previous Page</a>”;

This line of code uses the echo function to print the link in the browser screen. The line of HTML code, href=’javascript:history.back(1);’, will send the user to that screen which they where previously on. I’ll get into this later in the Javascript Tutorial.

/* If there was an error found, the program will jump down here and print out the errors on the users browser window. */
} else { // Report all errors

Remember way back in the code when we had this if statement:

if (isset($_POST['submitted'])) {
$errors = array(); // Create an Error Array

Check back for an explanation of this code. We if the variable submitted didn’t contain a value you would have been sent to this else statement. If the answer was true, then you will skip all of the code that follows the word else and is between the symbols { and }.

echo ‘<h2>An Error Occured</h2>
<p>The Following Error Occured: <br />’;

foreach ($errors as $msg) { // Show each Error
echo ” – $msg<br />\n”;
}

Here we are using the echo function again to write on the visitors screen. As you can see, everything between the quotes following the word echo will be printed on the screen, even if there is a newline in the code. The for each function will sort through the error box or array and if an error is found it will assign that error message to the variable msg and output that error message on the visitors screen.

/* Creates a link the visitor can click to go back to the original page */
echo “<a href=’javascript:history.back(1);’>Click Here to go Back to the Previous Page</a>”;
}
} // Close the Post Submitted Check

?>

A comment is used. Then we use the echo function to create the same link on the visitor’s screen. We use the symbol } twice to close all of the blocks of code. Then to tell the browser that it will not be receiving PHP code that it will have to analyze we close the PHP tag.

</body>
</html>

Here we close the whole PHP / HTML page like before. I hope this tutorial was understandable. If you have any questions leave them in the comment section below.

Here is the code, without my comments. Till next time…

<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* This program will be sent the data from the HTML file formtest2.htm.
It will make sure the required variables have a value set.
If not it will return an error message.
It will create variables and assign the data sent to the correct variables.
It will print on the visitors screen those values sent.
It will assign all of the data sent to a variable named message.
Then email the message to the chosen email address.
If any errors occured, it will make the visitor aware of how to correct the errors. */

/* Check if the form was submitted */
if (isset($_POST['submitted'])) {
$errors = array(); // Create an Error Array

/* Check to see if email, subject and message variables have a value */
if (empty($_POST['email'])) {
$errors[] = ‘Please Enter an Email.’;
}

if (empty($_POST['subject'])) {
$errors[] = ‘Please Enter a Subject.’;
}

if (empty($_POST['message'])) {
$errors[] = ‘Please Enter a Message.’;
}

/* Check to see if any required data was not entered. If an error occured, report that to the visitor */
if (empty($errors)) {

/* All form fields are automatically passed to the PHP script through the array $_POST. Then they are assigned to new PHP variables.*/
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$textinput = $_POST['textinput'];
$password = $_POST['password'];
$radioinput = $_POST['radioinput'];
$checkboxinput = $_POST['checkboxinput'];
$selectinput = $_POST['selectinput'];
$optioninput = $_POST['optioninput'];

/* Print out the information that the user inputted */
echo ” Email: $email <br />”;
echo ” Subject: $subject <br />”;
echo ” Message: $message <br />”;
echo ” Text Input: $textinput <br />”;
echo ” Password Input: $password <br />”;
echo ” Radio Input: $radioinput <br />”;
echo ” Checkbox Input: $checkboxinput <br />”;
echo ” Select Input: $selectinput <br />”;
echo ” Option Input: $optioninput <br />”;

/* Assign all of the visitor inputted data to the variable named message */
$message = ‘Text Area: ‘ . $message . ‘ Text Input: ‘ . $textinput . ‘ Password Input: ‘ . $password . ‘ Radio Input: ‘ . $radioinput . ‘ Checkbox Input: ‘ . $checkboxinput . ‘ Select Input: ‘ . $selectinput . ‘ Option Input: ‘ . $optioninput;

/* Sends the mail and outputs the “Thank you” string if the mail is successfully sent, or the error string otherwise. */
mail($email,$subject,$message);

echo “Message Sent, Thank you <br />”;

/* Creates a link the visitor can click to go back to the original page */
echo “<a href=’javascript:history.back(1);’>Click Here to go Back to the Previous Page</a>”;

/* If there was an error found, the program will jump down here and print out the errors on the users browser window. */
} else { // Report all errors

echo ‘<h2>An Error Occured</h2>
<p>The Following Error Occured: <br />’;

foreach ($errors as $msg) { // Show each Error
echo ” – $msg<br />\n”;
}
/* Creates a link the visitor can click to go back to the original page */
echo “<a href=’javascript:history.back(1);’>Click Here to go Back to the Previous Page</a>”;
}
} // Close the Post Submitted Check

?>
</body>
</html>

3 Responses to “PHP Form Mail Script”

  1. awesome stuff, cheers man

  2. Excellent. Thanks so much for the post. -Kate

  3. hospedagem says:

    this post will help me a lot for my work in college.

Leave a Reply

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Switch to our mobile site