<?php
/*
说明: 获取邮件内容
方法: Pop3 () 构造函数 $getMail = new Pop3("pop.qq.com","5847999","*****",110); $getMail -> getEmails(); $getMail -> getEmail(10);
login() 登陆方法
logoff() 退出方法
_strip_clf() 处理文本方法 一个私有的 为了测试 没有定义成私有
getEmailHeaders() 获取邮件头
getEmail($num) 获取用户输入邮件条数内容
getEmails() 调用这个以后获取所有文件 返回一个数组 打印数组查看内容 然后操作数组
foreachr($getMail -> $massages as $msg)
{
$msg -> to;
...
...
...
...
}
开发:andy
*/
class Pop3 {
var $_server; //服务器地址
var $_userName; //用户名称
var $_passWord; //用户密码
var $_port; //端口
var $_connection; //全局 连接
var $messages; //消息数组
var $mailCount; //邮件总数
function Pop3($server, $userName, $passWord, $port = 110) { //初始化构造函数
$this->_server = $server;
$this->_userName = $userName;
$this->_passWord = $passWord;
$this->_port = $port;
$this->messages = array();
}
function login() { // 登陆服务器方法
if (!empty($this->_server)) {
$this->_connection = @fsockopen($this->_server, $this->_port); //打开邮件服务器
$connectionResponse = @fgets($this->_connection,512);
if ($this->_connection) { //如果链接成功
@fputs($this->_connection, "USER " . $this->_userName."/r/n"); //发送用户名
$userResponse = @fgets($this->_connection,512); //发送密码
@fputs($this->_connection, "PASS ".$this->_passWord."/r/n");
$passResponse = @fgets($this->_connection,512);
if(strstr($passResponse, "+OK")) { //返回信息中包含 +OK 说明登陆服务器成功
$this->getEmailHeaders(); //获取所有头部 调用方法
$this->mailCount = count($this->messages); // 获取EMIAL的总数
return true;
}
}
}
return false;
}
function logoff() { // 退出方法
@fwrite($this->_connection, "QUIT/r/n");
$reply = fgets($this->_connection, 512);
$reply = $this->_strip_clf($reply);
@fclose($this->_connection);
unset($this->_connection);
}
function _strip_clf ($text = "")
{
// 返回字符串的 /r /n的替换
if(empty($text)) { return $text; }
$stripped = ereg_replace("/r","",$text);
$stripped = ereg_replace("/n","",$stripped);
return $stripped;
}
function getEmailHeaders() { //获取所有头部的方法
@fputs($this->_connection,"STAT/r/n"); //发送状态命令
$statResponse = split(" ",@fgets($this->_connection,512));
if($statResponse[0] != "+OK")
return false; //"391";
for($i = 0; $i < ($statResponse[1]); $i++)
{
$actualNumber = $i+1;
@fputs($this->_connection, "LIST $actualNumber/r/n");
$listResponse = @fgets($this->_connection,512);
$header = "";
fputs($this->_connection,"TOP $actualNumber 0/r/n");
while( ($rs = fgets($this->_connection,512) ) != "./r/n")
$header .= $rs; // 把邮件内容装到变量中
$this->messages[$i] = new Pop3Message($header); // 每个邮件都封装成一个对象
}
}
function getEmail($num)
{ // 获取EMAIL的调用方法
$message = Array();
$actualNumber = $num+1;
@fputs($this->_connection,"RETR $actualNumber/r/n"); //发送内容命令到服务器
$retrResponse = @fgets($this->_connection,512);
if (strstr($retrResponse,"+OK"))
{
while(($rs = @fgets($this->_connection,512))!="/r/n") {}
while(($rs = @fgets($this->_connection,512))!="./r/n") $message[] = trim($rs);
//$this->deleteEmail($actualNumber);
}
else
{
//错误信息
}
$this->messages[$num]->_parseEmail(null,$message);
}
function getEmails() //获取全部EMAIL
{
for ($i = 0; $i < $this->mailCount; $i++)
{
$this->getEmail($i);
}
}
function deleteEmail($num) { // 删除指定的EMAIL ID 的方法
@fputs($this->_connection,"DELE $num/r/n");
$del = split(" ", @fgets($this->_connection,512));
}
}
?>
需要其他三个类的 朋友 留下邮箱~~~