PHP Email

In this four-part series of articles we are going to build an email client with PHP. I will try to implement a full email client with all the functionality that you would expect from an email client, which includes a trash folder, sent items folder and a draft folder. But that’s not all.

We will also try to deal with the one issue that you don’t see in other PHP email client tutorials, which are attachments. I have searched the Internet for examples of how to find attachments in a mail message and could not find any. So I’ve decided to work it out myself and integrate it into this email client. We will use MySQL to store all the email data. In fact, the mail information will all be retrieved from the database.

Tools you need

You will need a copy of PHP 4.x MYSQL. Also, make sure that you have imap enabled on your PHP installation. You do this simply by removing the semicolon in the “Extensions” section of your PHP-ini file.

{mospagebreak title=How it Works}

I will be using PHP’s imap functions to write this client. There are over sixty functions available that you can use to create a mail client for both POP3 and IMAP. Visit http://www.php.net/ to see what functions are available. We will be creating a mail client that supports POP3 because it is by far the most popular mail protocol used today. To send an email message with PHP is very simple. You just use PHP’s mail() function. The syntax for the function is:

mail( string to, string subject, string message [, string
additional_headers [, string additional_parameters]] )

But because we want to give the user the choice to send attachments with email, we are going to use the freely available PHPMailer class (from http://phpmailer.sourceforge.net ). This class gives us the power to send email messages with all kinds of features, such as attachments, HTML messages or just plain messages. Although I will be using PHPMailer in this article, I will not be using it extensively, so please look at the following sites to learn more about this class:

Exampleshttp://phpmailer.sourceforge.net/extending.html

Tutorialhttp://phpmailer.sourceforge.net/tutorial.html

To retrieve email messages from the mail server, we will be using the @imap_headerinfo() and imap_msgno( ) functions. The imap_headerinfo() function retrieves all the message headers such as from, subject, and so forth, and the imap_msgno() function identifies the message number as it is numbered on the server.

To connect to the mail server we use the imap_open($mbox, $userid, $userpassword) function. As you can see this function takes three parameters, the mailbox name, the user name and user password.

The application will provide the ability to do the following:

  • User authentication.
  • Connect to the mail server and download and process messages.
  • Store the messages in the database one by one.
  • Retrieve and display all the newly downloaded messages.

After the messages are displayed on the index page, the program will give the user the option to view a message (this will lead to the view.php page) or to delete the message.

{mospagebreak title=The Database}

When the application downloads messages from the server, it will immediately send them to the database and then retrieve them to show on the index page. So in effect, you will be viewing messages that are retrieved from a database instead of messages that are directly downloaded from a mail server.

In addition to storing messages, the database will be used to store all messages that are classed as trash, drafts and sent items. So as you can appreciate, the database is very important to this application. Below I’ve listed all of the tables (along with the appropriate pieces of code) that are used by this application.

The contacts table will contain the names and email addresses of your contacts. It is linked to the users table through the userid foreign key, called “userid”:

CREATE TABLE `contacts` (
  `contact_id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default ”,
  `email` varchar(100) NOT NULL default ”,
  `userid` int(11) NOT NULL default ‘0’,
  PRIMARY KEY  (`contact_id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;
INSERT INTO `contacts` VALUES (1, ‘Joe Blogg’, ‘admin@mysite’,
1);
INSERT INTO `contacts` VALUES (2, ‘Joe Bloggs’, ‘admin@bloggs.com’, 1);

Next, the calendar table is used to store information about dates and times for events. The user is connected to this table by the foreign key called “userid.” This ensures that only entries made by this user are retrieved when requested.

CREATE TABLE `calendar` (
  `userid` int(11) NOT NULL default ‘0’,
  `calendar_id` int(11) NOT NULL auto_increment,
  `cal_date` date NOT NULL default ‘0000-00-00′,
  `entry` text NOT NULL,
  PRIMARY KEY  (`calendar_id`)
)  TYPE=MyISAM AUTO_INCREMENT=3 ;

INSERT INTO `calendar` VALUES (1, ‘2005-12-08′, ‘This encompasses more than the OED definition of a conflict as “a state of opposition or hostilities” or the “clashing of opposed principles”. Horowitz’s definition includes the desire to achieve something by the struggle, as well as the need for prevailing over an ‘other': both elements of ethnic conflict. rnrnThere are a great many theories attempting to explain why groups find themselves in conflict situations. With ethnic groups in particular, why is it so difficult to find resolutions before violence occurs, and why are people so willing to give up so much, often their lives, for their ethnic group? This presentation deals with conflict between ethnic groups primarily, and it is for ethnic groups that this phenomenon of putting the interests of the group before those of the individual is so widespread. rn’);

INSERT INTO `calendar` VALUES (2, ‘2005-12-28′, ‘This encompasses more than the OED definition of a conflict as “a state of opposition or hostilities” or the “clashing of opposed principles”. Horowitz’s definition includes the desire to achieve something by the struggle, as well as the need for prevailing over an ‘other': both elements of ethnic conflict. rnrnThere are a great many theories attempting to explain why groups find themselves in conflict situations. With ethnic groups in particular, why is it so difficult to find resolutions before violence occurs, and why are people so willing to give up so much, often their lives, for their ethnic group? This presentation deals with conflict between ethnic groups primarily, and it is for ethnic groups that this phenomenon of putting the interests of the group before those of the individual is so widespread. rn’);

The purpose of the Drafts table is to store all the mail messages that you want to write but don’t want to send immediately.  It also links the user through the foreign key.

CREATE TABLE `drafts` (
  `draft_id` int(11) NOT NULL auto_increment,
  `to` int(200) NOT NULL default ‘0’,
  `from` varchar(100) NOT NULL default ”,
  `subject` varchar(200) NOT NULL default ”,
  `msg_body` text NOT NULL,
  `userid` int(11) NOT NULL default ‘0’,
  `cc` varchar(100) NOT NULL default ”,
  `bcc` varchar(100) NOT NULL default ”,
  `attachment` varchar(200) NOT NULL default ”,
  PRIMARY KEY  (`draft_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

{mospagebreak title=More Tables}

The Messages table is at the heart of the application.  It is responsible for storing all the mail messages as they are downloaded from the server. This is where it all begins. The userid identifies the user that is downloading the messages and is stored in the userid foreign key.

CREATE TABLE `messages` (
  `msg_id` int(11) NOT NULL auto_increment,
  `msg_num` int(11) NOT NULL default ‘0’,
  `from` varchar(100) NOT NULL default ”,
  `subject` varchar(200) NOT NULL default ”,
  `msg_date` datetime NOT NULL default ‘0000-00-00 00:00:00′,
  `msg_body` text NOT NULL,
  `checked` int(1) NOT NULL default ‘0’,
  `userid` int(11) NOT NULL default ‘0’,
  PRIMARY KEY  (`msg_id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

INSERT INTO `messages` VALUES (1, 2, ‘admin@lo.com’, ‘helloooo’,
‘2005-12-20 00:00:00′, ‘<h1>Hello Jack</h1><br>rn<br>rn<p>This is a <em>cool</em>
day</p>rnrn<font color=”#FF000″>The End</font>’, 1, 1);

INSERT INTO `messages` VALUES (2, 1, ‘rach@look.co.uk’,
‘hellllllllooooo2′, ‘0000-00-00 00:00:00′,
‘dhdghfghdrnfghdfghdfgh’, 1, 1);

The sent and trash tables will store all the sent items and messages classed as trash. Both contain the foreign key that will identify the user of the system.

CREATE TABLE `sent` (
  `sent_id` int(11) NOT NULL auto_increment,
  `to` int(200) NOT NULL default ‘0’,
  `from` varchar(100) NOT NULL default ”,
  `subject` varchar(200) NOT NULL default ”,
  `msg_body` text NOT NULL,
  `userid` int(11) NOT NULL default ‘0’,
  `date_sent` date NOT NULL default ‘0000-00-00′,
  `cc` varchar(100) NOT NULL default ”,
  `bcc` varchar(100) NOT NULL default ”,
  `attachment` varchar(255) NOT NULL default ”,
  PRIMARY KEY  (`sent_id`)
)
CREATE TABLE `trash` (
  ` trash_id ` int(11) NOT NULL auto_increment,
  `to` int(200) NOT NULL default ‘0’,
  `from` varchar(100) NOT NULL default ”,
  `subject` varchar(200) NOT NULL default ”,
  `msg_body` text NOT NULL,
  `userid` int(11) NOT NULL default ‘0’,
attachment` varchar(255) NOT NULL default ”,
  PRIMARY KEY  (`trash_id`)
)

The “users” table is also another important table. It will help to store user details and will also be used by almost all scripts to retrieve user related information from the database.  The user ID is set at login and is used throughout the application.

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL auto_increment,
  `uname` varchar(100) NOT NULL default ”,
  `upass` varchar(50) NOT NULL default ”,
  `email` varchar(90) NOT NULL default ”,
  `remuser` varchar(200) NOT NULL default ”,
  `rempass` varchar(200) NOT NULL default ”,
  `pop3` varchar(200) NOT NULL default ”,
  `smtp` varchar(255) NOT NULL default ”,
  PRIMARY KEY  (`user_id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

INSERT INTO `user` VALUES (1, ‘yourname’, ‘pass’, ‘me@asite.com’,’ me@asite.com’, ‘yourpass’, ‘mail.asite.com’, ‘mail.asite.com’);

Copy and paste the above SQL into your database client and run the SQL. All the tables should now be created and ready for use.

Conclusion

This was just an introduction to how the application is going to function. In the next article we will deal with the login system.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye