Lesson 6 - PHP Mailer
In this tutorial I create a form that will be used for email. The action on the form will call the mailer php file that is residing on the server to read the values and then send an email and display the values in html. Email Form The email form, emailform.html, is created by opening a blank HTML file. Next the form is populated with textfields and a textarea. A close link is added because this file will be a popup window called from another HTML file using a link. The link in the calling HTML file is:
This popup window will also serve as the location where the php file writes out it's HTML displaying what the user has typed in.
The action field is set to the domain where the php file resides. In this case it the domain is http://www.myDomain.com and the php file is mailer.php. myDomain will be where you host the php file. If you do not have a host go to either www.freehostia.com or www.leadhoster.com to get a very inexpensive host. The free versions do not support SMTP mail. You must have a host that will allow you to send email and will run php. If it does not then the mailer will not work. It seems like it works best when the server is a linux server.
Note that it is important to set the name and id to the same value for browser compatibility. These values will be used in the php file to read what the user has typed in.
Dissecting the PHP Mailer The mailer.php file is called from the action field in the form. This is like an HTML file but instead of .htm or .html it's extension is .php which tells the server to use php parsing of the script inside of it. To add php scripting to the php file the starting and ending php tags, <?php and ?>, must be added in the <body> of the .php file:
I broke the mailer into two parts. In the first part, below, the values of the fields in the form are read and the email is generated. Four php variables are set which will be sent using the php mail function.
Note that the variable names in the brackets following the $_POST command is the same as those in the name and id on the form. Since the method in the form used post, then POST must be used in the php file. Using post is more secure than using get and it is not restricted in the number of characters that may be sent.
After all these values have been extracted and put into the php variables they are passed to the mail function which sends the email.
In the second part the HTML is generated that will show the user what they typed into the form. A table is used to display the name of the form fields and their values. A loop is used to simplify the code.
Using the Email Form After the emailform.html file and the mailer.php file are copied to the host I bring up the emailform in the browser and enter all the fields and press Submit. The form disappers and the HTML page generated by php will appear with all of the values that were input. At the bottom is a close line that the php file created.
An email containing the values of the form is sent and the following are the contents of that email.
Copyright © 2008 - 2009 Robert D. Cormia - October 31, 2008 |