Вот класс(ещё ниже пример использования):
class getmail //Use imap its the best
{
var $socket = -1;
var $status;
function getmail( $server, $port, $username, $password )
{
if( $this->gm_connect( $server, $port ) )
{
if( $this->gm_user( $username ) )
{
$this->gm_pass( $password );
}
}
}
function gm_command( $command )
{
if( $this->socket )
{
$command = $command . "\r\n";
fputs( $this->socket, $command );
$line = fgets( $this->socket, 1024 );
$this->status[ "lastresult" ] = substr( $line, 0, 1 );
//echo $this->status[ "lastresult" ]."<br>";
$this->status[ "lastresultmsg" ] = $line;
//echo $this->status[ "lastresultmsg" ]."<br>";
if($this->status[ "lastresult" ] != "+" ) return 0;
}
else
{
return 0;
}
return 1;
}
function gm_connect( $server, $port )
{
$this->socket = @fsockopen( $server, $port) or die("ВАЖНО: Не могу подконектиться!!!!") ;
if( !$this->socket ) return 0;
$line = fgets( $this->socket, 1024 );
$this->status[ "lastresult" ] = substr( $line, 0, 1 );
//echo $this->status[ "lastresult" ]."<br>";
$this->status[ "lastresultmsg" ] = $line;
//echo $this->status[ "lastresultmsg" ]."<br>";
if( $this->status[ "lastresult" ] != "+" ) return 0;
return 1;
}
function gm_user( $username )
{
$command = "USER " . $username;
$result = $this->gm_command( $command );
if( !result )
{
fclose( $this->socket );
$this->socket = -1;
}
return $result;
}
function gm_pass( $password )
{
$command = "PASS " . $password;
$result = $this->gm_command( $command );
if( !result )
{
fclose( $this->socket );
$this->socket = -1;
}
return $result;
}
function gm_stat()
{
$this->gm_command( "STAT" );
if( !eregi( "+OK (.*) (.*)", $this->status[ "lastresultmsg" ], $result ) ) return 0;
return $result[1];
}
function gm_list()
{
$this->gm_command( "LIST" );
if( $this->status[ "lastresult" ] != "+" ) return 0;
$i = 0;
while( substr( $line = fgets( $this->socket, 1024 ), 0, 1 ) != "." )
{
$mailbox[ $i ] = $line;
$i++;
}
$mailbox[ "messages" ] = $i;
return $mailbox;
}
// return all we need
function gm_retrieve( $message_id )
{
$command = "RETR " . $message_id;
$this->gm_command( $command );
if( $this->status[ "lastresult" ] != "+" ) return 0;
$i = 0;
$header = 1;
while( substr( $line = fgets( $this->socket, 1024 ), 0, 1 ) <> "." )
{
if( !$header )
{
$body[ $i ] = $line;
$i++;
}
else
{
if( substr( $line, 0, 6 ) == "Date: " )
{
$date = substr( $line, 6 );
}
else if( substr( $line, 0, 6 ) == "From: " )
{
$from = substr( $line, 6 );
}
else if( substr( $line, 0, 9 ) == "Subject: " )
{
$subject = substr( $line, 9 );
}
else if( substr( $line, 0, 4 ) == "To: " )
{
$to = substr( $line, 4 );
}
}
if( ( $header == 1 ) && ( strlen( $line ) == 2 ) )
{
$header = 0;
}
}
$body[ "lines" ] = $i;
return new message( $body, $date, $from, $subject, $to );
}
function gm_quit()
{
return $this->gm_command( "QUIT" );
fclose( $this->socket );
$this->socket = -1;
}
}
class message
{
var $body;
var $date;
var $from;
var $subject;
var $to;
function message( $body, $date, $from, $subject, $to )
{
$this->body = $body;
$this->date = $date;
$this->from = $from;
$this->subject = $subject;
$this->to = $to;
}
}
Использование класса:
это нужно вводить из формы:
$HTTP_POST_VARS["server"]
$HTTP_POST_VARS["port"]
$HTTP_POST_VARS["user"]
$HTTP_POST_VARS["pass"]
<?php
if( !empty($HTTP_POST_VARS["server"]) and !empty($HTTP_POST_VARS["port"]) and
!empty($HTTP_POST_VARS["user"]) and !empty($HTTP_POST_VARS["pass"]))
{
// use class_gm
$gm = new getmail($HTTP_POST_VARS["server"], $HTTP_POST_VARS["port"],
$HTTP_POST_VARS["user"], $HTTP_POST_VARS["pass"] );
$mailbox = $gm->gm_list();
if( $mailbox[ "messages" ] > 0 )
{
echo "<br>
<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#FFFFFF'>
<tr>
<td width='100%' background='img/bg.gif'>Количество сообщений: ".$mailbox[ "messages" ]."</tb></tr></table><br>
";
for( $i = 1; $i < $mailbox[ "messages" ] + 1; $i++ )
{
// print number of message
$message = $gm->gm_retrieve( $i ); // retrieve information
if( !$message )
{
echo "ВАЖНО: что-то произошло с письмом номер: $i<br>";
}
else {
// subject
echo "<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#FFFFFF'>
<tr> <td width='50%' background='img/bg.gif'>Тема: ";
for( $j = 0; $j < $message->body[ "lines" ]; $j++ )
{
echo $message->subject[$j]."";
}
echo "</td>";
// date name
echo "<td width='50%' background='img/bg.gif'>Написанно:";
for( $j = 0; $j < $message->body[ "lines" ]; $j++ )
{
echo $message->date[$j]."";
}
echo "</td></tr>";
// you message
echo "<tr> <td colspan='2' background='img/bg1.gif'>";
for( $j = 0; $j < $message->body[ "lines" ]; $j++ )
{
echo "".$message->body[$j]."<br>";
}
echo "</td></tr>";
// to who???
echo "<tr> <td width='50%' background='img/bg.gif'>Кому:";
for( $j = 0; $j < $message->body[ "lines" ]; $j++ )
{
echo $message->to[$j]."";
}
echo "</td>";
// from who???
echo "<td width='50%' background='img/bg.gif'>От кого:";
for( $j = 0; $j < $message->body[ "lines" ]; $j++ )
{
echo $message->from[$j]."";
}
echo "</td></tr></table><br>
";
}
}
}
else
{
echo "ВАЖНО: ваш ящик пуст<br>";
}
$gm->gm_quit();
}
else
{
echo "ВАЖНО: Введите данные<br>";
}
?>