Thread: Email handling
View Single Post
  #3 (permalink)  
Old October 10th, 2003, 01:39 PM
richard.york's Avatar
richard.york richard.york is offline
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

In PHP there are two routes to take for developing your own mail client.

The first is using the IMAP function library.
The IMAP function library is an extra module and must be compiled into PHP before the functions will be available. (or activated in PHP.ini in Windows and provided the correct path to the .dll).

Don't be fooled by the name, this set of functions may be used to access IMAP, POP3 or NNTP mail protocols.

Read more about IMAP here:

The second route is designing custom mail handling functions. Which would require setting up sockets and sending specific commands to the mail server. This would be the most time consuming and most portable approach as the top method requires the c-client module to be installed on the server.

You can run a check to see if the server already has the IMAP module installed by running phpinfo().

As Nik mentioned it's a rather time consuming process to create your own mail client. There are loads of tutorials out there that utilize either method. Just do a google search or use one of the many already built mail clients out there.

Here are a couple of functions that I wrote using the IMAP module.

    # array imap_connect(void)
    # The imap_open function may be used to open a connection to the mail server

    function imap_connect() {

        $mail = array();

         *    for pop3: {}INBOX 
         *    and for imap: {}INBOX.

        $mailbox_server = "{}INBOX";                

        $mail["connection"] = @imap_open($mail_data["mailbox_server"], $mail_data["mailbox_user"], $mail_data["mailbox_password"]);


        return $mail;


    # bool close_imap_connection(array mail components)
    # imap_close closes the mail connection

    function close_imap_connection($mail) {

        if (!@imap_close($mail["connection"])) {

            echo "Error: unable to close mail connection.<br />\n";

            return false;

        } else {

            return true;



    # bool imap_error_message(resource handler)
    # imap_errors(), imap_alerts() 
    # display any errors brought back from the mail server

    function imap_error_message($result) {

        if (empty($result)) {            

            $errors = imap_errors();
            $alerts = imap_alerts();

            if (is_array($alerts)) {

                for ($i = 0; each($alerts); $i++) {

                    if (isset($alerts[$i])) {

                        echo $alerts[$i]."<br />\n";                        




            if (is_array($errors)) {

                for ($i = 0; each($errors); $i++) {

                    if (isset($errors[$i])) {

                        echo $errors[$i]."<br />\n";




            return true;


        return false;


    # int imap_message_count(void)
    # imap_num_msg - returns the message count

    function imap_message_count() {    

        $mail = imap_connect();

            if (!$message_count = @imap_num_msg($mail["connection"])) {

                $message_count = "0";



        return $message_count;


    echo "You have ";
    echo imap_message_count();
    echo " messages total!";
I haven't checked for errors, I just did a breif modification of functions that I already use! Let me know if you have any more questions with specific components and I'll do my best to help out.

Best of luck!
: )

Smiling Souls
Reply With Quote