Page 1 of 1

Making a webmail system | PHP | Part 2 Rate Topic: -----

#1 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Posted 01 April 2012 - 08:12 AM

Tutorial: making a webmail system in PHP

Sorry for the big delay in getting part 2 of the tutorial up here. The next part will be a lot quicker I assure you.

Welcome to the second part of the tutorial on how to make a basic webmail system with PHP and MySQL. Just to recap here is the list of all the features that the finished e-mail system will have:

- Retrieve e-mails from a specified e-mail account
- Show all the mailboxes for the account
- Retrieve and save e-mail attachments
- Save all e-mails to a database for security
- Have a preview screen below the e-mail listings
- Compose e-mail function
- The ability to delete, reply and forward messages

This follows on from part one click here if you have not already seen it. So far in this tutorial series I have covered the server and the database setup for the programme.

Tutorial Part 2 - Basic Classes

In this tutorial I am going to get down into the basics of any good system, the basic classes that will control the whole programme. In this part I am only going to cover the database class, as even though it is not the best way to do it, the code to connect to the e-mail server will be created into a page and tested, and then I will explain how to turn it all into a class.

Step 1: It is a good idea to have the file architecture setup before we start, below is the way that I would organise it. This is in addition to the folders in the previous tutorial.

#Root
Mail/
Template/
Images/
Classes/
classes/database/
classes/e-mail/
Pages/
index.php

As we go through the tutorials we will be adding files to all those folders.

Step 2: Create a file called database.class.php in classes/database and copy the code below into it and save. I will explain it all in detail after the complete code.

Important note: not all the code below is using the new php functions like built in iterations of mysqli and preprepared statements. This database code has been written to be easy to read and understand feel free to update it to include the above features

<?php
// The database class which will be used in this system
class database{

  // Setup global class variables
  var $connection;
  var $querys;
  var $error;

  // The on init function which will connect to the database
  // Passes in all required login information to increase compatibility
  // All information must be base64_encoded and put into the script for security reasons
  function database($server,$user,$password,$database){
    $this->connection = mysqli_connect(base64_decode($server),base64_decode($user),base64_decode($password),base64_decode($database));
  }

  // The query function - this will only return a 1 or 0
  function query($q){
    $this->querys = mysqli_query($this->connection,$q);
    if(!mysqli_error($this->connection)){
      return 1;
    }else{
      $this->error = mysqli_error($this->connection);
      return 0;
    }
  }
  
  // The num rows function returns the number of rows returned by the query
  function nrows(){
    return mysqli_num_rows($this->querys);
  }
  
  // This result function - returns an array with all the results
  function results(){
    // Array varliable
    $res = "";
    // Check to see if there is at least 1 result
    if($this->nrows() >= 1){
      // Counter
      $i = 0;
      while($row=mysqli_fetch_array($this->querys)){
        $res[$i] = $row;
        $i++;
      }
      // return array
      return $res;
    }else{
      // return nothing
      return "";
    }
  }

  // The close connection function - closes mysqli connection and resets all variables
  function close(){
   mysqli_close($this->connection);
   $this->connection = "";
   $this->querys = "";
   $this->error = "";
  }
  
  // END OF CLASS

}
?>




As I am sure you appreciate this is a long script but will make the whole job easier and will speed up the whole application. In this tutorial I am going to be using the latest in MySQL code mysqli which is widely accepted. I am not going to go into detail about this class as there are a lot of database tutorials already on DIC.

Webmail Template

In order to keep things tidy in the file structure we are going to put the template in a template folder called Template. Remember all images should have Template/ at the beginning of the src to ensure they are displayed. On the next part of the tutorial we will be creating a template parsing system which will parse all the shortcodes used in the template.

Step One: All the shortcodes we will need:
{folders}, {message}, {from}, {subject},{bcc},{cc},{attachments},{date} and {messagelist}

All the other features like reply etc will be in the template itself. We also need a message list page which contains one row as a template for the list. So that page would have shortcodes: {subject}, {from} and {date}

Here is our folder structure

#Root
Mail/
Template/
template/images
template/main.php
template/messagelist.php
Images/
Classes/
classes/database/
classes/e-mail/
Pages/
index.php

I am only going to use basic html tables to make this tutorial simple you can use div's and css of course if you wish.

main.php
<html>
    <head>
	<title>Webmail</title>
    </head>
    
    <body>
          <table style="border: 0px;">
                 <tr>
                      <td rowspan="3">{folders}</td>
                      <td>
                           <table style="border: 0px;">
                                  {messagelist}
                           </table>
                      </td>
                 </tr>
                 <tr>
                      <td>
                            <table style="border: 0px;">
                                   <tr>
                                        <td>From: </td>
                                        <td>{from}</td>
                                   </tr>
                                   <tr>
                                       <td>CC: </td>
                                       <td>{CC}</td>
                                   </tr>
                                   <tr>
                                       <td>BCC: </td>
                                       <td>{BCC}</td>
                                   </tr>
                                   <tr>
                                       <td>Date: </td>
                                       <td>{date}</td>
                                   </tr>
                                   <tr>
                                        <td>Attachments: </td>
                                        <td>{attachments}</td>
                                   </tr>
                            </table>
                      </td>
                 </tr>
                 <tr>
                     <td>{message}</td>
                 </tr>
          </table>
    </body>
 </html>
 


messagelist.php
 <tr>
     <td>{subject}</td>
     <td>{from}</td>
     <td>{date}</td>
 </tr>
 


That is all the templates we need for the basic webmail system. As we get further into development we will add more templates for writing e-mails, replying to e-mails etc. So in the next tutorial coming in the next week or so we will start writing the e-mail class which will allow us to check the e-mail using pop/imap connections.

Until next time.

This post has been edited by jonesa01: 02 April 2012 - 02:21 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Making a webmail system | PHP | Part 2

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Posted 10 April 2012 - 02:40 AM

Can I just suggest that rather than teaching PHP4-style code to use PHP5 methodology, since you're using MySQLi anyway.

Here's a couple of tips:
Don't use var, use the appropriate access modifier.
Use MySQLi in it's OOP format, since you're using it in a class anyway.
Use __construct for your constructor.
Was This Post Helpful? 0
  • +
  • -

#3 JuanCarrion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 14

Posted 18 May 2014 - 05:38 AM

When is the next part of this tutorial coming out???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1