Lesson 6 - PHP Mailer

 

welcome to Wreamweaver 8

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:

<a href="emailform.html"
onclick="window.open(this.href, 'popupwindow','width=620,height=660,scrollbars=yes');return false;">Send an Email</a>

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.

Email Form

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.

Form Action

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.

Form Fields

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:

<?php
php scripting goes here
?>

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.

  • fwdemail is set to your email address
  • msg is a concatenation of the time stamp, the name, from, and message values of the fields in the mailer form
  • subject is a contatenation of a tag that you may set to anything that will help you in filtering it from other emails, and the subject field on the form
  • headers is set to the value of the From field.

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.

  • $_POST['name']
  • $_POST['email']
  • $_POST['subject']
  • $_POST['message']

After all these values have been extracted and put into the php variables they are passed to the mail function which sends the email.

Php Mailer

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.

Php Mailer HTML generation

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.

Php Mailer HTML

An email containing the values of the form is sent and the following are the contents of that email.

Email sent by Php File

Copyright © 2008 - 2009 Robert D. Cormia - October 31, 2008