mail.pop |  |
mail.pop -- Библиотека функций для работы с протоколом POP3
Некоторые вопросы использования этой библиотеки (обсуждение в форуме):
Как пользоваться библиотекой mail.php?
Странная кодировка в теме и в имени отправителя письма: “=?Windows-1251?Q?=F2=E5=F1=F2=EE=E2=EE=E5=”
http://popoff.donetsk.ua/forum/podskazhete.html
Эта библиотека была создана мной на основе класса pop3class:
http://www.phpclasses.org/pop3class
Отличия:
Исходный текст | |
<?php
// libs/mail/mail.pop.php
// (c) Yuri Popoff, Jun 2004, popoff.donetsk.ua
// A set of functions to check mail via pop3
// Based on a pop3class:
// http://www.phpclasses.org/pop3class
function _mail_pop_connection_close(&$data)
{
if(DEBUG_MODE)
$data['protocol'].="Closing connection.\n";
if($data['_fp'])
fclose($data['_fp']);
$data['_fp']=0;
}
function _mail_pop_connection_open(&$data,$host,$port)
{
if(empty($host))
{
trigger_error('_mail_pop_connection_open: hostname is empty');
return false;
}
if(DEBUG_MODE)
$data['protocol'].='Connecting '.$host."...\n";
$fp=fsockopen($host,$port,&$error,&$errstr);
if(!$fp)
{
trigger_error("_mail_pop_connection_open: could not connect [".$host."]: ".$error." ".$errstr);
return false;
}
return $fp;
}
function _mail_pop_read(&$data,$check='')
{
if(feof($data['_fp'])) return false;
$line=rtrim(fgets($data['_fp']));
if($line===false)
{
trigger_error('_mail_pop_read: Could not retrive server response');
return false;
}
if(DEBUG_MODE)
$data['protocol'].='< '.$line."\n";
$s=$line;
if($check&&_mail_pop_tokenize($line,' ')!=$check)
{
trigger_error('_mail_pop_read: invalid response: '.$s);
return false;
}
return $line;
}
function _mail_pop_tokenize(&$s,$separator)
{
if($s=='') return '';
$p=strlen($s);
for($i=0;$i<strlen($separator);$i++)
{
if(($c=strpos($s,$separator[$i]))!==false)
$p=min($p,$c);
}
$r=substr($s,0,$p);
if($p>=strlen($s)-1)
$s='';
else
$s=substr($s,$p+1);
return $r;
}
function _mail_pop_write(&$data,$line)
{
if(DEBUG_MODE)
$data['protocol'].='> '.$line."\n";
if(!fputs($data['_fp'],$line."\r\n"))
{
trigger_error("_mail_pop_write: could not send command: ".$line);
return false;
}
return true;
}
// Close method - this method must be called at least if there are any
// messages to be deleted
function mail_pop_close(&$data)
{
if($data['_state']=="DISCONNECTED")
{
trigger_error('mail_pop_close: connection was not opened');
return false;
}
// if($data['_must_update'])
// {
if(!_mail_pop_write($data,"QUIT"))
{
trigger_error('mail_pop_close: could not send the QUIT command');
return false;
}
$s=_mail_pop_read($data);
if(!$s||_mail_pop_tokenize($s,' ')!='+OK')
{
trigger_error('mail_pop_close: invalid QUIT command response');
return false;
}
// }
_mail_pop_connection_close($data);
$data['_state']='DISCONNECTED';
return true;
}
function mail_pop_create()
{
return array(
//private - do not access
'_fp' => 0,
'_greeting' => '',
'_must_update' => 0,
'_state' => 'DISCONNECTED',
//public
'protocol' => ''
);
}
// DeleteMessage method - the $message argument indicates the number of
//a message to be marked as deleted. Messages will only be effectively
//deleted upon a successful call to the Close method. */
function mail_pop_delete(&$data,$message)
{
if($data['_state']!="TRANSACTION")
{
trigger_error('mail_pop_list: connection is not in TRANSACTION state');
return false;
}
if(!_mail_pop_write($data,"DELE ".$message)) return false;
$s=_mail_pop_read($data,'+OK');
if(!$s) return false;
$data['_must_update']=true;
return true;
}
// ListMessages method - the $message argument indicates the number of a
// message to be listed. If you specify an empty string it will list all
// messages in the mail box. The $unique_id flag indicates if you want
// to list the each message unique identifier, otherwise it will
// return the size of each message listed. If you list all messages the
// result will be returned in an array. */
function mail_pop_list(&$data,$message='',$unique_id=true)
{
if($data['_state']!="TRANSACTION")
{
trigger_error('mail_pop_list: connection is not in TRANSACTION state');
return false;
}
if($unique_id)
$list_command="UIDL";
else
$list_command="LIST";
if(!_mail_pop_write($data,$list_command.' '.$message)) return false;
$s=_mail_pop_read($data,'+OK');
if($s===false) return false;
if(!$message)
{
$res=array();
while(1)
{
$s=_mail_pop_read($data);
if(!$s) return false;
if($s=='.') break;
$msg=intval(_mail_pop_tokenize($s,' '));
$id='';
$size=0;
if($unique_id)
$id=_mail_pop_tokenize($s,' ');
else
$size=intval(_mail_pop_tokenize($s,' '));
$res[]=array (
'msg' => $msg,
'size' => $size,
'id' => $id
);
}
return $res;
}
else
return array(
array(
'msg' => intval(_mail_pop_tokenize($s,' ')),
'size' => intval(_mail_pop_tokenize($s,' ')),
'id' => ''
)
);
}
// Login method - pass the user name and password of POP account. Set
// $apop to 1 or 0 wether you want to login using APOP method or not.
function mail_pop_login(&$data,$user,$password)
{
if($data['_state']!="AUTHORIZATION")
{
trigger_error('mail_pop_login: connection is not in AUTHORIZATION state');
return false;
}
if(!empty($data['_greeting'])&&!empty($data['use-apop']))
{
if(!_mail_pop_write($data,"APOP $user ".md5("<".$data['_greeting'].">".$password))) return false;
$s=_mail_pop_read($data);
if(!$s||_mail_pop_tokenize($s,' ')!='+OK')
{
$data['protocol'].="APOP login failed";
return false;
}
}
else
{
if(!_mail_pop_write($data,"USER ".$user))
{
trigger_error('mail_pop_login: Could not send the USER command');
return false;
}
$s=_mail_pop_read(&$data);
if(!$s||_mail_pop_tokenize($s,' ')!='+OK')
{
$data['protocol'].="USER command failed";
return false;
}
if(!_mail_pop_write($data,"PASS ".$password))
{
trigger_error('mail_pop_login: Could not send the PASS command');
return false;
}
$s=_mail_pop_read(&$data);
if(!$s||_mail_pop_tokenize($s,' ')!='+OK')
{
$data['protocol'].="PASS command failed";
return false;
}
}
$data['_state']="TRANSACTION";
return true;
}
/* IssueNOOP method - Just pings the server to prevent it auto-close the
connection after an idle timeout (tipically 10 minutes). Not very
useful for most likely uses of this class. It's just here for
protocol support completeness. */
function mail_pop_noop(&$data)
{
if($data['_state']!="TRANSACTION")
{
trigger_error('mail_pop_noop: connection is not in TRANSACTION state');
return false;
}
if(!_mail_pop_write($data,'NOOP')) return false;
$s=_mail_pop_read($data,'+OK');
if($s===false) return false;
return true;
}
function mail_pop_open(&$data,$host,$port=110)
{
if($data['_fp']||$data['_state']!='DISCONNECTED')
{
trigger_error('mail_pop_open: already connected');
return false;
}
$fp=_mail_pop_connection_open($data,$host,$port);
if(!$fp) return false;
$data['_fp']=$fp;
$s=_mail_pop_read($data,'+OK');
if($s===false)
{
trigger_error('mail_pop_open: POP3 server greeting is invalid');
return false;
}
_mail_pop_tokenize($s,'<');
$data['_greeting']=$s;
$data['_state']='AUTHORIZATION';
$data['_must_update']=0;
return true;
}
// ResetDeletedMessages method - Reset the list of marked to be deleted
//messages. No messages will be marked to be deleted upon a successful
//call to this method. */
function mail_pop_reset(&$data)
{
if($data['_state']!="TRANSACTION")
{
trigger_error('mail_pop_noop: connection is not in TRANSACTION state');
return false;
}
if(!_mail_pop_write($data,'RSET')) return false;
$s=_mail_pop_read($data,'OK+');
if(!$s) return false;
$data['_must_update']=false;
return true;
}
// RetrieveMessage method - the $message argument indicates the number of
// a message to be listed. Pass a reference variables that will hold the
// arrays of the $header and $body lines. The $lines argument tells how
// many lines of the message are to be retrieved. Pass a negative number
// if you want to retrieve the whole message. */
function mail_pop_retrieve(&$data,$message='',$lines=-1)
{
if($data['_state']!='TRANSACTION')
{
trigger_error('mail_pop_retrieve: connection is not in TRANSACTION state');
return false;
}
if($lines<0)
{
if(!_mail_pop_write($data,'RETR '.$message)) return false;
}
else
{
if(!_mail_pop_write($data,'TOP '.$message.' '.$lines)) return false;
}
$s=_mail_pop_read($data,'+OK');
if($s===false) return false;
$res=array('headers' => array(), 'body' => '');
$h='';
while(true)
{
$s=_mail_pop_read($data);
if($s===false) return false;
if(!$s) break;
if($s=='.') return $res;
if($s[0]==' '||$s[0]=="\t")
$h.="\n".trim($s);
else
{
if($h)
$res['headers'][]=$h;
$h=trim($s);
}
}
if($h)
$res['headers'][]=$h;
while(true)
{
$s=_mail_pop_read($data);
if($s===false) return false;
if($s=='.') break;
$res['body'].="\n".$s;
}
$is_quoted_printable=false;
for($i=0;$i<count($res['headers']);$i++)
if($res['headers'][$i]=='Content-Transfer-Encoding: quoted-printable')
{
$is_quoted_printable=true;
break;
}
if($is_quoted_printable)
{
$res['body']=quoted_printable_decode($res['body']);
}
return $res;
}
// Statistics method - pass references to variables to hold the number of
//messages in the mail box and the size that they take in bytes.
function mail_pop_statistics(&$data)
{
if($data['_state']!="TRANSACTION")
{
trigger_error('mail_pop_statistics: connection is not in TRANSACTION state');
return false;
}
if(!_mail_pop_write($data,"STAT"))
{
trigger_error('mail_pop_statistics: Could not send the STAT command');
return false;
}
$s=_mail_pop_read(&$data);
if(!$s||_mail_pop_tokenize($s,' ')!='+OK')
{
$data['protocol'].="STAT command failed";
return false;
}
return array(
'messages' => _mail_pop_tokenize($s,' '),
'size' => _mail_pop_tokenize($s,' ')
);
}
?>
Последняя модификация: 13.11.06 19:15 q Не проходите мимо! Оставьте Ваш комментарий в форуме! >>> Цитирование материалов моего сайта приветствуется! при условии видимой действующей! гиперссылки на мой сайт. [Ссылки] Если Вы нашли опечатку на этой странице, пожалуйста, выделите ее мышью и нажмите Ctrl+Enter. Сделаем язык чище! (c) Yuri Popoff, 2004 - 2008, popoff.donetsk.ua, style.donetsk.ua |
|