Welcome to the first in a series of tutorials in which I will talk about how to make a basic webmail system with PHP and MySQL.
The finished system will have the following system:
- 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
I will go through it at a steady pace while explaining things thoroughly and giving you the code, breakdowns and links to the php site about the functions used.
Tutorial Part 1 Server Setup
For this system you will need a PHP server, a SMTP/IMAP server (or Gmail etc.) and a MySQL database.
Step 1: setup the location where you will make the script either a subdomain or a folder. This depends on your host and where you want people to access the facility.
Step 2: setup your database, I advise you if possible to have a separate database to make sure the e-mail data is secure as possible. Keep the server url, username, password and database handy as you will need them throughout this and the next part of this tutorial.
Step 3: add the initial database tables to the database using the sql code below. In this example it will store both the e-mail address/username and password (encrypted) in a database and store all the e-mails in another. The other table will store all the mailboxes for each e-mail address so the webmail can be loaded without connecting to the mail server if you wanted.
SQL: users table stores email addresses and passwords
CREATE TABLE `users`( `ID` INT(250) NOT NULL AUTO INCREMENT, `Email` VARCHAR(250) NOT NULL, `Password` VARCHAR(250) NOT NULL, Primary key(`ID`));
SQL: mailbox table stores all the mailboxes for a user uses user ID as a secondary key
CREATE TABLE `mailboxes`( `ID` INT(250) NOT NULL AUTO INCREMENT, `Name` VARCHAR(250) NOT NULL, `UserID` INT(250) NOT NULL, Primary key(`ID`));
SQL: message table stores all the messages for each user stores Mailbox ID as a secondary key
CREATE TABLE `messages`( `ID` INT(250) NOT NULL AUTO INCREMENT, `To` VARCHAR(250) NOT NULL, `CC` VARCHAR(250), `BCC` VARCHAR(250), `From` VARCHAR(250) NOT NULL, `Subject` VARCHAR(250), `Message` LONG BLOB NOT NULL, `Date` DATETIME NOT NULL, `MailboxID` INT(250) NOT NULL, Primary Key (`ID`));
I have not covered the database for the attachments as that will come in later on in the tutorial series when we get to the stage of working with attachments.
Step 4: setup the folders you will need below is the basic file organisation you will need:
End of Tutorial 1
I have not covered much in this tutorial but the aim is to get the foundations together so we are ready to build on the script.
In the next tutorial I will be concentrating on the database class and running a script to test it before we get down to the real code on this project.
Until next time..