Developing
a PHP mail script for your web site feedback form
1page 2
page
There is something about feedback forms,
something I consistently fail to realize. The thrill and excitement
of watching web viewers send information over the Internet,
and knowing you control the entire process is utterly invigorating.
Perhaps it's the PHP language I enjoy using so much. Perhaps
it's the e-mail I enjoy receiving and responding to. In any
event, PHP feedback forms are powerful utilities, and this
article will teach you how to develop and create one all your
own.
Before we begin, if you don't have PHP installed
on your web server, point your browser to php.net
and download yourself a free copy for either Windows or Linux.
Once installed, you're ready to experience how easy it is
to create a mail processing script in PHP. Let's take a look
at how variables are used.
Your variables
Simply put, your form element names will
automatically be created as variables by PHP, and will be
populated with the value that the user has inputted, or selected,
or clicked, etc. Below is an example of a simple text box
named 'Lastname'.
<input type="text"
Name="Lastname" Value="">
So, PHP will create a variable named 'Lastname'
and will fill it with the information the user supplied the
text box with. To recall the variable within the mail script,
simply place a dollar sign in front of the variable. In this
case, to call the value of Lastname, simply use $Lastname.
Let's look at how we'll use form variables with PHP's mail()
command.
The mail() command
The mail() command is the most basic and
simplest way of sending e-mail, which contacts your system's
smtp server as specified in PHP's configuration files. The
command looks like this:
mail(to, subject, body, optional_headers);
Optional headers can contain a 'From' address,
for example. There are a couple ways you can setup the mail
script. First, let's initialize each variable, then put those
variables in the mail() command.
Example 1.1
| <?php
$mailto = "your@address.com";
$msgSubject = "Your subject";
$msgBody = "Variables here, like $Name,
$Comments, etc";
mail($mailto, $msgSubject, $msgBody,
"From: your@address.com");
?>
|
|
First, we have declared and initialized three variables, $mailto,
$msgSubject and $msgBody. Remember, within the message body,
only include variables which you have asked for in your actual
form, and keep in mind PHP is case sensitive, so $Name is
different from $name. We then popped those variables into
PHP's mail() command, along with an additional header, From.
So, 'your@address.com' will receive an e-mail with a subject
of 'Your Subject' and a body that'll read 'Variables here,
like $Name, $Comments, etc'. Of course, the variables $Name
and $Comments will be parsed, and the value behind those variables
will be sent in place of the variable name.
Within the body, you may want to skip a line
or two for readability of the message. That is easily done
with the \n command. So, taking the above example, if you
wanted a line space in-between $Name and $Comments, try this:
"Variables here, like
$Name, \n $Comments, etc"
Simple enough? Now, let's take a look at another example
using the mail() command but without initializing any variables
first.
Example 1.2
| <?php
mail("your@address.com",
"Your subject here", "Variables
here, like $Name, $Comments, etc");
?>
|
|
Both examples will yield the same result, but the code technique
comes down to what your personal preferences are. You may
take this code and play around with it until you get exactly
what you're looking for.
Let's take this a step further. What happens
if a visitor wants to play around and submit a bunch of blank
forms? What if you want only certain form fields to be required,
and the form will not submit unless data has been supplied
to those fields? Go ahead and move on to my next
article in this series which checks field integrity.
Author: Steve
WebSite: http://www.websitegravy.com |