自己写的IMAP类 用POP3
头一回写类,应该可以减肥下,不过懒...
不好的地方记得要留言喔<?php
class imap{
var $name;
var $domainName;
var $password;
var $mailbox;
var $username;
var $linkServer;
function imap($name,$domainName,$password){
$this->name = $name;
$this->domainName = $domainName;
$this->password = $password;
$this->mailbox = "{pop3.$domainName:110/pop3}";
$this->username = $name.'@'.$domainName;
}
function open(){
$this->linkServer = @imap_open($this->mailbox,$this->username,$this->password);
}
function checkOpen(){
if(imap_ping($this->linkServer) === false){
$error = 'imap_open的连接错误:'.imap_last_error();
return $error;
}else{
$str = 'ok';
return $str;
}
}
function ND(){
$checkResult = imap_check($this->linkServer);
$numMsgs = $checkResult->Nmsgs;
$date = $checkResult->Date;
$checkReturn[] = $numMsgs;
$checkReturn[] = $date;
return $checkReturn;
}
function head($id = '',$all = false,$several = array()){
if($all == true){
$checkReturn = imap::ND();
$numMsgs = $checkReturn[0];
for($i=0;$i<$numMsgs;$i++){
$head_array[$i]['id'] = $i+1;
$head = imap_header($this->linkServer,$i+1);
if($head->udate == ''){
$head_array[$i]['udate'] = '';
}else{
$head_array[$i]['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array[$i]['subject'] = '';
}else{
$head_array[$i]['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array[$i]['subject']);
$head_array[$i]['subject'] = $analyze_charset[0]->text;
}
if($head->to == ''){
$head_array[$i]['to'] == '';
}else{
$to = $head->to;
$to = $to[0]->mailbox.'@'.$to[0]->host;
$head_array[$i]['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array[$i]['to']);
$head_array[$i]['to'] = $analyze_charset[0]->text;
}
if($head->from == ''){
$head_array[$i]['personal'] == '';
$head_array[$i]['from'] == '';
}else{
$from = $head->from;
$head_array[$i]['personal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array[$i]['personal']);
$head_array[$i]['personal'] = $analyze_charset[0]->text;
$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array[$i]['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array[$i]['from']);
$head_array[$i]['from'] = $analyze_charset[0]->text;
}
}
return $head_array;
}
if(count($several) > 0){
$num = count($several);
for($i=0;$i<$num;$i++){
$head_array[$i]['id'] = $several[$i];
$head = imap_header($this->linkServer,$several[$i]);
if($head->udate == ''){
$head_array[$i]['udate'] = '';
}else{
$head_array[$i]['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array[$i]['subject'] = '';
}else{
$head_array[$i]['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array[$i]['subject']);
$head_array[$i]['subject'] = $analyze_charset[0]->text;
}
if($head->to == ''){
$head_array[$i]['to'] == '';
}else{
$to = $head->to;
$to = $to[0]->mailbox.'@'.$to[0]->host;
$head_array[$i]['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array[$i]['to']);
$head_array[$i]['to'] = $analyze_charset[0]->text;
}
if($head->from == ''){
$head_array[$i]['personal'] == '';
$head_array[$i]['from'] == '';
}else{
$from = $head->from;
$head_array[$i]['personal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array[$i]['personal']);
$head_array[$i]['personal'] = $analyze_charset[0]->text;
$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array[$i]['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array[$i]['from']);
$head_array[$i]['from'] = $analyze_charset[0]->text;
}
}
return $head_array;
}
$head = imap_header($this->linkServer,$id);
$head_array['id'] = $id;
if($head->udate == ''){
$head_array['udate'] = '';
}else{
$head_array['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array['subject'] = '';
}else{
$head_array['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array['subject']);
$head_array['subject'] = $analyze_charset[0]->text;
}
if($head->to == ''){
$head_array['to'] == '';
}else{
$to = $head->to;
$to = $to[0]->mailbox.'@'.$to[0]->host;
$head_array['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array['to']);
$head_array['to'] = $analyze_charset[0]->text;
}
if($head->from == ''){
$head_array['personal'] == '';
$head_array['from'] == '';
}else{
$from = $head->from;
$head_array['personal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array['personal']);
$head_array['personal'] = $analyze_charset[0]->text;
$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array['from']);
$head_array['from'] = $analyze_charset[0]->text;
}
return $head_array;
}
function analyze_body($id){
$structure = imap_fetchstructure($this->linkServer,$id);
return $structure;
}
function show_section($id){
$structure = imap::analyze_body($id);
$subtype = $structure->subtype;
if($subtype == 'PLAIN'){
$text = imap_fetchbody($this->linkServer,$id,1);
$encode = $structure->encoding;
$size = $structure->bytes;
switch($encode){
case 3:
$text = base64_decode($text);
break;
case 4:
$text = imap_qprint($text);
break;
}
$section[] = $text;
$section[] = $size;
$section['attachment'] = '';
return $section;
}
if($subtype == 'ALTERNATIVE'){
$text = imap_fetchbody($this->linkServer,$id,2);
$size = $structure->bytes;
$parts = $structure->parts;
$encode = $parts[1]->encoding;
switch($encode){
case 3:
$text = base64_decode($text);
break;
case 4:
$text = imap_qprint($text);
break;
}
$section[] = $text;
$section[] = $size;
$section['attachment'] = '';
return $section;
}
if($subtype == 'MIXED'){
$size = $structure->bytes;
$parts = $structure->parts;
$parameters = $parts[0]->parameters;
if($parameters[0]->attribute != 'BOUNDARY'){
$text = imap_fetchbody($this->linkServer,$id,1);
$encode = $parts[0]->encoding;
switch($encode){
case 3:
$text = base64_decode($text);
break;
case 4:
$text = imap_qprint($text);
break;
}
$attachment_size = $parts[1]->bytes;
if($parts[1]->ifdparameters == 1 || $parts[1]->ifparameters == 1){
$dparameters = $parts[1]->dparameters;
$filename = $dparameters[0]->value;
}else{
$parameters = $parts[1]->parameters;
$filename = $parameters[0]->value;
}
$analyze_charset = imap_mime_header_decode($filename);
$filename = $analyze_charset[0]->text;
$details = imap_fetchbody($this->linkServer,$id,2);
$attachment_encode = $parts[1]->encoding;
switch($attachment_encode){
case 3:
$details = base64_decode($details);
break;
case 4:
$details = imap_qprint($details);
break;
}
$section[] = $text;
$section[] = $size;
$section['attachment']['filename'] = $filename;
$section['attachment']['details'] = $details;
$section['attachment']['size'] = $attachment_size;
return $section;
}else{
$boundary = $parameters[0]->value;
$text = imap_fetchbody($this->linkServer,$id,1);
$text = explode($boundary,$text);
$text = $text[2];
$start = strpos($text,'<',0);
$length = strlen($text);
$text = substr($text,$start,$length);
$parts2 = $parts[0]->parts;
$encode = $parts2[1]->encoding;
switch($encode){
case 3:
$text = base64_decode($text);
break;
case 4:
$text = imap_qprint($text);
break;
}
$attachment_size = $parts[1]->bytes;
if($parts[1]->ifdparameters == 1 || $parts[1]->ifparameters == 1){
$dparameters = $parts[1]->dparameters;
$filename = $dparameters[0]->value;
}else{
$parameters = $parts[1]->parameters;
$filename = $parameters[0]->value;
}
$analyze_charset = imap_mime_header_decode($filename);
$filename = $analyze_charset[0]->text;
$details = imap_fetchbody($this->linkServer,$id,2);
$attachment_encode = $parts[1]->encoding;
switch($attachment_encode){
case 3:
$details = base64_decode($details);
break;
case 4:
$details = imap_qprint($details);
break;
}
$section[] = $text;
$section[] = $size;
$section['attachment']['filename'] = $filename;
$section['attachment']['details'] = $details;
$section['attachment']['size'] = $attachment_size;
return $section;
}
}
}
function setFlag($id,$status){
imap_setflag_full($this->linkServer,"$id","$status");
}
function clearFlag($id,$status){
imap_clearflag_full($this->linkServer,"$id","$status");
}
function search($string){
$array = imap_search($this->linkServer,$string);
$num = count($result);
$result[] = $array;
$result[] = $num;
return $result;
}
function delete(){
if(imap_expunge($this->linkServer)){
return true;
}else{
return false;
}
}
function full_header($id){
$result = imap_fetchheader($this->linkServer,$id);
return $result;
}
function download($filename,$details){
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$filename");
echo $details;
}
function close(){
imap_close($this->linkServer);
}
}
function dump($vars, $label = '', $return = false)
{
if (ini_get('html_errors')) {
$content = "<pre>\n";
if ($label != '') {
$content .= "<strong>{$label} :</strong>\n";
}
$content .= htmlspecialchars(print_r($vars, true));
$content .= "\n</pre>\n";
} else {
$content = $label . " :\n" . print_r($vars, true);
}
if ($return) { return $content; }
echo $content;
return null;
}
?> ;