PHPBuilder - Email Forms in PHP



RSS Twitter
Articles Php Functions

Email Forms in PHP

by: Dan Ball
|
August 22, 2007

Barebones PHP basics

A couple very basic concepts of PHP must be discussed before getting into this project further. First, PHP code is inserted into pages inside <?php and ?> tags. Second, any page that contains any amount of those tags should have the a file extension of either .php, .php3, .php4, or .php5. The .php3 extension is for pages that are using features that were new to version 3 of PHP, and so on. Check with your system administrator for any rules regarding which extension you need to use and the tags you need to enclose the code, because PHP is also designed to be custom configured to fit the way you are comfortable coding. It can be configured to use the ASP <% %> tags amongst other customizations that are beyond the scope of this article.
PHP pages can still contain a lot of typical HTML, so your pages will still need the basic <html>, <body>, etc. tags. They just may not be right at the top like you are used to seeing because there may be PHP variables and such being defined before the <html> tag. Also, if you do a view source at the client level, you will only see the HTML and client-side scripting, because by that time all the PHP has been read and processed by the server and the resulting HTML is fed to the browser.
With all that being understood, let's get on with the fun stuff.

The Form

Like any email form, the original form itself is quite simple. No great shakes here, just a standard HTML form with a POST method and the action pointing to the PHP page that will process the form results.
<form name="form" method="post"
  action="contact_thanks.php">
<p class="bodymd">Your Name<br>

<input type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" rows="5" cols="40">
</textarea>

</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</p>
</form>
The file extension for the page this form is on can be .html, .shtml or whatever you choose; the page that the results are handed to, however, must be a PHP page.

Processing the Mail

Let's t ake a look at the code [ PHP code for email form. ]. It is very typical of any scripting language — server-side or client-side, so if you have any amount of knowledge in JavaScript, ASP, JSP, Perl or the like, it should seem very familiar.
The code is a combination of validation and email processor. Take a look at the first if statement, it is checking to see if any of the fields are empty. If any one field is, it then opens a form and prints a statement to inform the user that they missed a required field(s).
The statements after that are also simple. The if statements check each of the individual fields to see if they are empty; if they are empty, the script prints that field again to be filled in (note that the form was already opened above if a field was empty). If it is not empty it then stores the value of that field in a hidden field.
The final if statement above is again checking for empty fields. If any of the required fields are empty, it will print submit and clear buttons as well as the closing form tag to complete the form that has been opened earlier for re-submission. Once re-submitted it will be re-validated by the same process. Once all of the required fields are filled in, it then hits the else option of the last if statement. This is where the mail is processed and sent.

Compiling and Sending the Email

Look at the else in the final if statement:
else
{
	$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
	$extra = "From: $Name\r\nReply-To: $Email\r\n";
	mail ("noone@nowhere.com", "Website Email", $message, $extra);
	echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>";
	echo "<p class=bodymd>A response will be sent to
   $Email as soon as possible.</p>";
}
The first line sets the body of the email. This is where it takes the values of all the fields, which are variabilized with the field name preceded by a "$," so the field name "Name" is known to PHP as $Name. The \n starts the text on the next line. Therefore, $message = "Name: $Name\nEmail: $Email\nComments: $Comments\n"; will result in this message body:
Name: {value of Name field}
Email: {value of Email field}
Comments: {value of Comments field}
The second line is not necessary to send email, but it is convenient to use, as it sets the "from" and "reply-to" fields in your email client. It's a neat feature.
Now sending the email, you won't believe how easy this is. All it takes is:
mail ("noone@nowhere.com", "Website Email", $message, $extra);
The "mail" statement sends the defined values to the sendmail program configured in your Web server. The first value is where the email gets sent to, the second is the subject of the email, the third is the body of the message that you complied above, and the last is the "from" and "reply-to" statement from above ... thats it!
Now how easy was that?!

Final Thoughts

PHP has really renewed my interest in my profession. It has promise of adding new life to my Web sites. I have been working with MySQL integration and administration as well as permissions and password protection. All tasks have been enjoyable, interesting and relatively quick and easy. I really think any real Web geek like myself will find great fun in learning the powerful features and functions that PHP has to offer, and on top of it all, PHP, MySQL and Apache are all free! If that doesn't help to spark your curiousity level, I doubt anything will.

About the author:

Dan Ball is very involved in Web development and the music industry and has produced recordings, streaming media, and Web sites for several musical artists worldwide. Currently he operates his own audio production and Web development business, dB Masters Audio Productions. He can be contacted at dbmasters@excite.com.
This article originally appeared on WebReference.com

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Dan Ball

Comment:



Comment:

(Maximum characters: 1200). You have characters left.