April 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Web mail in PHP

  • April 16, 1999
  • By Bjxrn Borud
  • Send Email »
  • More Articles »
Asked to write an article about Apache and PHP, I had to take a moment to come up with a good sample application to demonstrate the beauty of Apache and PHP. It had to be something that I would personally like to use but simple enough for educational purposes, and something at least marginally original.

A Web-based e-mail interface may not be a very original idea, nor is my humble example any match for the more advanced Web mail applications that already exist, but I'm likely to need such an interface at some point and it makes an excellent topic for showing off what you can do with PHP. So Web mail it is.

What is PHP?

In short, PHP is a server-side programming environment that lets you mix HTML and script code. The scripting language resembles Perl and if you know a little Perl or C you will find it quite easy to get started using PHP. Since PHP was specifically designed to be a server-side scripting language for Web servers, it offers a vast array of functions and behind-the-scenes magic to make common tasks easy. For instance, data entry fields in forms will automagically turn up as PHP variables that use the same names as the form fields etc.

PHP also supports a number of databases, ranging from big names like Oracle, Sybase and Informix to smaller database systems like MySQL -- the latter being perhaps the most popular database to use in conjunction with PHP.

Enough talk!

I'm sure you're dying to get a taste, so let's jump right into some simple code.

As I mentioned earlier PHP lets you mix HTML and script code. Now what does this look like in practice? Consider the following stripped-down file:
   <TITLE>Sample PHP script</TITLE>
   <H1>Date sample</H1>

   <?PHP
     $today = date("Y-m-d");
     print "Today's date is $today\n";
   ?>

   The above date was printed by PHP.

As you can see, we start out with some standard HTML code. We provide a title for the page and a heading. Then we encounter section of the document enclosed within "<?PHP" and "?>". These are called SGML processing tags -- this is how we tell the Web server where there's code that we wish to run.

The code doesn't do much. First it calls the

date()
function with some parameters to tell it how we would like the current date formatted. The result of
date()
is assigned to variable
$today
and on the next line we print the result.

Forming content

As mentioned earlier, PHP was specifically designed as a server-side scripting language to make life easier for Web developers. Something a lot of people ask about is how to handle FORM content, so let's see how that can be done in PHP. Consider the following HTML code that contains a form:
   <TITLE>Form example</TITLE>
   <FORM ACTION=formhandler.php3 METHOD=GET>
      Name: <INPUT TYPE=text NAME=name VALUE=""><BR>
     Email: <INPUT TYPE=text NAME=email VALUE=""><BR>

     <INPUT TYPE=submit>
   </FORM>

The ACTION parameter points to a file with the peculiar suffix

php3
. This is the file containing the code that handles the form content when we click the submit button. The code for this handler might be something like this:
   <TITLE>Form handler example</TITLE>
   Mailing the data to the webmaster:

   <?PHP
      $message = "Name: $name\nEmail: $email\n";
      mail("your-email-here", "Form feedback", $message);
      print "The following data was sent:\n $message";
   ?>

Remember that we had two input fields in our form, named "name" and "email"? The values we typed into these input fields are now assigned to PHP variables with the same names as the form input fields and we can manipulate them just like any other variable. In this particular case we combine them into a string and use a built-in function to mail the data to the Webmaster.

Going from here to there

We could spend all afternoon looking at cute little examples like that, but that's probably not why you came. You're wondering whether this PHP thing can be used for something useful and worthwhile.








Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel