Email Forms In PHP, The Easiest Yet...by: Dan BallFull Version Article published Saturday, 27th September 2003 PHP Email Form Introduction I recently admitted to myself that my home business should be moved off of the "free hosting" service I have had it on for about 3 years. Though the service was great, and it was very feature-rich as compared to any other free service I have seen, it is still subject to invasive maneuvers by the company itself. That said, I moved to a pay service. The immediate thrill was no banner code to add to my pages. The underlying benefit was their support of PHP (Hypertext Preprocessor) pages and hosting a MySQL database. I knew when I signed up that they supported that, what I didn't know was how great that combination was, and how powerful PHP really is. With this "rebirth" I have found in web development with my introduction to PHP I have decided to dedicate a little bit of my time, and a few pages to PHP and how easy it is to setup a very cool email feedback form with it. My last contribution to WebReference.com was a tutorial in the basics of JavaMail, which, in it's own right, is very cool and still much easier than the ol' Perl/CGI forms of yester-year. However, I do feel that PHP even has a slight edge on JavaMail in the fact that PHP has, at least to this point, been much easier to learn and implement some very handy features into my website. A simple feedback form being the first I accomplished. 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. Though specific servers can be set up differently, this is the standard. Second, any page that contains any amount of those tags MUST have the .php or .php3 extension. The .php3 extension is for pages that are using features that were new to version 3 of PHP. Check with your system administrator for any rules regarding which extension you need to use, or if both are OK. The PHP pages still contain a lot of typical HTML, so your pages will still need the typical <html>, <body>, etc. 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 tag. Also, like JSP and ASP, 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> This form 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 Take a look at the code below. 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, you are going to look at this and it will be very familiar. The code is a combination of validation and if validated, the email processor as well. 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 say they did miss some required field(s). The statements after that are also simple if statements that check each individual fields to see if they are empty, if they are empty, it prints that field again to be filled in (note that the form was already opened above if a field was empty as well), if it is not empty it then stores the value of that field in a hidden field. <?php if (($Name == "") || ($Email == "") || ($Comments == "")) if (($Name == "") || ($Email == "") || ($Comments == "")) 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 to re-submit the form. 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. 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 inquiry, $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, as most of you I am sure know, 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 cool, as it sets the "from" and "reply-to" fields in your email client. It's a neat feature. Now sending it, 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 you 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 that you complied above, and the last is the "from" and "reply-to" statement from above...thats it! can you believe that! The statements following that are simply printing a "thank you" statement to the browser to tell the visitor that the email was successfully sent. 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 websites. 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. As I learn and grow with PHP I hope to add more articles of this nature. I think anyone would find PHP an interesting and rewarding project. Related Stories: » Protecting your PHP and HTML Source Code » Publishing Newsletters Using PHP & MySQL - 4 » Publishing Newsletters Using PHP & MySQL - 3 » Publishing Newsletter Using PHP & MySQL - 2 » Publishing Newsletters Using PHP & MySQL » Unix Webserver Crontab Basics |