Customer
Support Center
TUTORIAL 1
Mail Formatter
This is a tutorial for those of you that want to learn PHP/FI but
prefer to learn using a working example instead of only reading
the documentation.
The source code for the applet "mail.html" can be found
at http://pagehome.com/faq/phpmail.txt.
It is recommended that you print this out to aid you in the
tutorial.
Click here to see
a working version of the applet.
The object of the applet is to allow you to give your name,
e-mail address, and and text you may want to write and it will be
mailed to you or some other e-mail address. In this case I've
modified the code to have the results e-mailed to whatever e-mail
address you give.
Before we get into the meat of the code, there are a few things
about PHP that you should know.
Syntax
Each PHP instruction starts with and ends with a >
or instructions may be grouped inside a single >pair and
separated by ; characters.
Variables are supported and are indicated by preceding the
variable name with a $. Examples can be found at http://www.vex.net/php/doc.phtml#vars
In addition to these there are 13 Language Constructs available.
They are:
- if(condition)
- else
- elseif(condition)
- endif
- switch(expression)
- case expression
- default
- break
- endswitch
- while
- endwhile
- include
- exit
The Code
Now, let's go line by line through the code to
see exactly what's happening.
Here are the first 9 lines:
thanks to thomas bullinger for writing the first sample php script mail form mail results to this address to="rasmus@vex.net" this is the mail program we will use check path on target system mp="/usr/lib/sendmail -t">
|
Example 1
First we are starting a script with which basically tells PHP
to parse any information after that until it encounters a >.
Then we make a comment beginning with /* and ending with */
to give credit where credit is due.
Next we define where the mail is going to be sent to. In this
example the mail is going to "rasmus@vex.net" but I
have modified this slightly so that the mail will be sent to
whoever fills out the form. Notice that after each line there is
a ;.
Then we define the mail program we will use to send the formatted
message. In this case it will be Sendmail.
The next three lines you will recognize as basic HTML code but
after that are 13 lines of code that are very important to the
mail formatter.
Message Sent!<p>
</body></html>
|
Example 2
The first line is an if statement. It's basically saying that if "sendit" is appended to the URL and $body has returned a value of true then proceed with the next line. What this does is makes sure you have filled out the required information. One thing to keep in mind is that we are working from front to back. Meaning, there are three pages involved with this script and we are creating the last page first, then the middle page, then the first page.
So if everything checks out then we proceed to line 2. $fd = popen(MP,"w");. We are saying that we want $fd to mean "open a pipe to a command and return a file pointer index".
The next 6 lines sends the data enclosed in quotes to $fd. Which means, we are sending the "To" "From" "Subject" "Reply-To" and "X-Mailer" information to the mail client of the person receiving the mail.
Line 7 closes the pipe we've opened on line 2. It also, if you'll notice, closes the subroutine with a >. Lines 8 and 9 are basic HTML.
Line 10 say's to exit from PHP and line 11 ends the "if" statement we started on line 1. So lines 2 through 12 aren't run until certain criteria are met. If they haven't been met then the script proceeds to the next lines.
Next 11 lines:
/* If the user has not filled out the body of the message yet, present the form */
if(!body)>
This is an example form written using the PHP script language. The
form results are formatted and e-mailed to a given address.<p>
<center>
<form action="" method="POST">
Your Name: <input type="text" name="name" size=40><br>
EMail Addr: <input type="text" name="EMAIL_ADDR" value=""
size=40><br>
Suggestions<br><textarea name="body" rows=4 cols=70></textarea><br>
<input type="submit" value="Submit">
</center>
|
Example 3
You can pretty much tell by the comment what line 2 is doing. The literal translation would be: If not body then display the following text and keep the same page
Then we define what text to display. Following that we give the form in which the name, e-mail address, and body will be filled in. Notice in the form action we reference to . This means that we will be posting back to the PHP script. $PHP_SELF is a pre defined variable built in to PHP and useable with any script.
In line seven you can see a perfect example of one of the great features of PHP. We give the input a name of EMAIL_ADDR. PHP automatically defines this as a variable for later use! `value="" ' means to place whatever you type in the form for this field to $EMAIL_ADDR. So in the form, where it asks for your e-mail address, whatever you put in there is given the value of $EMAIL_ADDR.
The rest is basic HTML.
The rest of the code:
This is what the complete message that is being sent looks like:<p>
<center><table border=1><tr><td>
</body></html>
|
Example 4
Now, this section of code is the "else" to the "if" we started before. The whole code say's IF the body has not been filled out then re-display this page ELSE display the next page. We are dealing with the else of that statement.
Line 2 is just text letting the user know what that page is. Line 3 starts a table that will contain a copy of data the user is sending.
Line 4 says to "echo" or send to PHP the data contained in the quotes. The same goes for lines 5 through 9 and line 10 just closes the table.
Line 12 says to post to PHP_SELF but this time append "?sendit" to the URL. PHP will start from the top of the script and see that sendit has been appended to the URL so it will execute the first 13 lines of code (Example 2).
In lines 13 and 14 the type equals "hidden" because we want PHP to carry the values of these to back to Example 2 when it passes back through it.
Line 17 ends the IF statement we started and line 18 closes the body and html and effectively finishes our program!
So that's pretty much it. If you compare this to formmail.cgi you'll see how much easier it can be to do the same thing in PHP as opposed to programming it in Perl.
If you have any questions feel free to e-mail me at support@pagehome.com
and I'll try to answer any questions you might have.
Top Of Page
Brought to you by:
Great Southern Resources,
Inc.
Copyright© 1997 All
rights reserved.
All other products mentioned are registered trademarks or
trademarks of their respective companies.
Questions or problems regarding this web site should be directed to webmaster@pagehome.com.