用PHP来发邮件,相信大家都不陌生,但读取收件箱的话,接触就少了,这次总结下自己的经验,希望可以帮助大家.
1.PHP读取收件箱主要是利用imap扩展,所以在使用以下方法前,必须开启imap扩展模块的支持.
var $marubox='';           
var $email='';    
function receiveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure
{
if($servertype=='imap')
{
if($port=='') $port='143';
$strConnect='{'.$mailserver.':'.$port. '}INBOX';
}
else
{
$strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
$this->server      =  $strConnect;
$this->username     =  $username;
$this->password     =  $password;
$this->email     =  $EmailAddress;
}
function connect() //Connect To the Mail Box
{
$this->marubox=@imap_open($this->server,$this->username,$this->password); 
if(!$this->marubox) 
{ 
  return false; 
//     echo "Error: Connecting to mail server";
//     exit;
}
return true;
} 
function getHeaders($mid) // Get Header info
{
if(!$this->marubox)
return false; 
$mail_header=imap_header($this->marubox,$mid); 
$sender=$mail_header->from[0]; 
$sender_replyto=$mail_header->reply_to[0]; 
if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster') 
{ 
  $subject=$this->decode_mime($mail_header->subject); 
  $ccList=array(); 
  foreach ($mail_header->cc as $k => $v) 
  { 
    $ccList[]=$v->mailbox.'@'.$v->host; 
  } 
  $toList=array(); 
  foreach ($mail_header->to as $k => $v) 
  { 
    $toList[]=$v->mailbox.'@'.$v->host; 
  } 
  $ccList=implode(",",$ccList); 
  $toList=implode(",$toList); 
  $mail_details=array( 
      'fromBy'=>strtolower($sender->mailbox).'@'.$sender->host,'fromName'=>$this->decode_mime($sender->personal),'ccList'=>$ccList,//strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,'toNameOth'=>$this->decode_mime($sender_replyto->personal),'subject'=>$subject,'mailDate'=>date("Y-m-d H:i:s",$mail_header->udate),'udate'=>$mail_header->udate,'toList'=>$toList//strtolower($mail_header->to[0]->mailbox).'@'.$mail_header->to[0]->host 
//         'to'=>strtolower($mail_header->toaddress)
);
}
return $mail_details;
}
function get_mime_type(&$structure) //Get Mime type Internal Private Use
{
$primary_mime_type = array("TEXT","MULTIPART","MESSAGE","APPLICATION","AUDIO","IMAGE","VIDEO","OTHER");  
if($structure->subtype && $structure->subtype!="PNG") {  
  return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;  
}  
return "TEXT/PLAIN";  
}
function get_part($stream,$msg_number,$mime_type,$structure = false,$part_number = false) //Get Part Of Message Internal Private Use
{  
if(!$structure) {  
  $structure = imap_fetchstructure($stream,$msg_number);  
}  
if($structure) {  
  if($mime_type == $this->get_mime_type($structure)) 
  {  
    if(!$part_number)  
    {  
      $part_number = "1";  
    }  
    $text = imap_fetchbody($stream,$part_number); 
    if($structure->encoding == 3) 
    { 
      return imap_base64($text); 
//         if ($structure->parameters[0]->value!="utf-8")
//         {
//           return imap_base64($text);
//         }
//         else
//         {
//           return imap_base64($text);
//         }
}
else if($structure->encoding == 4)
{
return iconv('gb2312','utf8',imap_qprint($text));
}
else
{
return iconv('gb2312',$text);
}
}
if($structure->type == 1) / multipart /
{
while(list($index,$sub_structure) = each($structure->parts))
{
if($part_number)
{
$prefix = $part_number . '.';
}
$data = $this->get_part($stream,$sub_structure,$prefix . ($index + 1));
if($data)
{
return $data;
}
}
}
}
return false;
}
function getTotalMails() //Get Total Number off Unread Email In Mailbox
{
if(!$this->marubox)
return false; 
//   return imap_headers($this->marubox);
return imap_num_recent($this->marubox);
} 
                                                (编辑:宣城站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!