Contact Site map Home

Technical Support

PHP/FI

TUTORIAL 2

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 phpmail.txt. It is recommended that you print this out to aid you in the tutorial.


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 $.

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.

Example 2

<?if($argv[0]=="sendit" isset($body)); fd="popen(MP,"w");" fputs($fd, to: to\n"); fputs($fd, from: email_addr\n"); fputs($fd, subject: form results\n"); fputs($fd, reply-to: email_addr\n"); fputs($fd, x-mailer: php-v2.0b2\n\n"); fputs($fd, body); pclose($fd)> Message Sent!<p> </body></html> <?exit; endif; pre>

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="<?echo php_self>" method="POST"> Your Name: <input type="text" name="name" size=40><br> EMail Addr: <input type="text" name="EMAIL_ADDR" value="<?echo email_addr>" 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 <?echo php_self>. 

This means that we will be posting back to the PHP sc ript. $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="<?echo email_addr>" ' 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:

<?else> This is what the complete message that is being sent looks like:<p> <center><table border=1><tr><td> <?echo to: to<br>"; echo from: email_addr<br>"; echo from: form results<br>"; echo from: email_addr<br>"; echo from: php-v1.99<br>"; echo lt;pre>$body</pre>"; lt;/td></tr></table></center> lt;ce nter> lt; form action="<?echo $PHP_SELF>?sendit" method="POST" gt; lt;input type="hidden" name="EMAIL_ADDR" value="<?echo $EMAIL_ADDR>" gt; lt;input type="hidden" name="body" value="<?echo $body>" gt; lt;input type="submit" value=" Looks ok, send it! " gt; lt;/center> endif> </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.
 
 
[ Home ]   [ About ]   [ Plans ]   [ Designs ]   [ Graphics ]   [ Marketing ]   [ Hosting ]   [ Portfolio ]   [ Contact ]   [ Site Map ]
Copyright © 1995-2000 XyNexT Internet Strategies - All Rights Reserved Worldwide