Previous Section  < Day Day Up >  Next Section

13.15 IMAP, POP3, and NNTP

You can write a full-featured mail or news client in PHP. (In fact, some people already have梒heck out http://www.horde.org/imp/ and http://www.squirrelmail.org/). The imap extension gives your PHP programs the ability to talk with IMAP, POP3, and NNTP servers. Example 13-20 uses some of the imap extension functions to connect to the news.php.net news server and retrieve information about 10 most recent messages from the php.announce newsgroup.

Example 13-20. Connecting to an NNTP server
$server = '{news.php.net/nntp:119}';

$group = 'php.announce';

$nntp = imap_open("$server$group", '', '', OP_ANONYMOUS);



$last_msg_id = imap_num_msg($nntp);



$msg_id = $last_msg_id - 9;



print '<table>';

print "<tr><td>Subject</td><td>From</td><td>Date</td></tr>\n";



while ($msg_id <= $last_msg_id) {

    $header = imap_header($nntp, $msg_id);



    if (! $header->Size) { print "no size!"; }



    $email = $header->from[0]->mailbox . '@' .

        $header->from[0]->host;

    if ($header->from[0]->personal) {

        $email .= ' ('.$header->from[0]->personal.')';

    }



    $date = date('m/d/Y h:i A', $header->udate);



    print "<tr><td>$header->subject</td><td>$email</td>" .

        "<td>$date</td></tr>\n";

$msg_id++;

}

print '</table>';

Example 13-20 prints:

<table><tr><td>Subject</td><td>From</td><td>Date</td></tr>

<tr><td>PHP Security Advisory: CGI vulnerability in PHP version 4.3.0</td>

<td>sniper@php.net (Jani Taskinen)</td><td>02/17/2003 01:01 PM</td></tr>

<tr><td>PHP 4.3.2 released</td><td>sniper@php.net (Jani Taskinen)</td>

<td>05/29/2003 08:05 AM</td></tr>

<tr><td>PHP 5.0.0 Beta 1</td><td>sterling@bumblebury.com (Sterling Hughes)</td>

<td>06/29/2003 02:19 PM</td></tr>

<tr><td>PHP 4.3.3 released</td><td>ilia@prohost.org (Ilia Alshanetsky)</td>

<td>08/25/2003 09:53 AM</td></tr>

<tr><td>PHP 5.0.0 Beta 2 released!</td><td>andi@zend.com (Andi Gutmans)</td>

<td>10/30/2003 03:57 PM</td></tr>

<tr><td>PHP 4.3.4 Released</td><td>ilia@prohost.org (Ilia Alshanetsky)</td>

<td>11/03/2003 08:25 PM</td></tr>

<tr><td>PHP 5 Beta 3 Released!</td><td>andi@zend.com (Andi Gutmans)</td>

<td>12/22/2003 05:48 AM</td></tr>

<tr><td>PHP 5 Release Candidate 1</td><td>andi@zend.com (Andi Gutmans)</td>

<td>03/18/2004 12:24 PM</td></tr>

<tr><td>PHP 4.3.5 Released</td><td>ilia@prohost.org (Ilia Alshanetsky)</td>

<td>03/26/2004 08:55 AM</td></tr>

<tr><td>PHP 4.3.6 Released</td><td>ilia@prohost.org (Ilia Alshanetsky)</td>

<td>04/15/2004 05:28 PM</td></tr>

Read about the imap extension in O'Reilly's PHP Cookbook, Recipes 17.3, 17.4, and 17.5; and in the PHP Manual (http://www.php.net/imap).

    Previous Section  < Day Day Up >  Next Section