加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP receiveMail实现收邮件功能

发布时间:2021-05-22 20:29:18 所属栏目:PHP教程 来源:网络整理
导读:副标题#e# 用PHP来发邮件,相信大家都不陌生,但读取收件箱的话,接触就少了,这次总结下自己的经验,希望可以帮助大家. 注意: 1.PHP读取收件箱主要是利用imap扩展,所以在使用以下方法前,必须开启imap扩展模块的支持. 2.此方法支持中文,不会乱码,需要保持所有文

//移动邮件到指定分组
function move_mails($msglist,$mailbox)
{
if(!$this->marubox)
return false;

imap_mail_move($this->marubox,$msglist,$mailbox); 

}

function creat_mailbox($mailbox)
{
if(!$this->marubox)
return false;

//imap_renamemailbox($imap_stream,$old_mbox,$new_mbox); 
imap_create($this->marubox,$mailbox); 

}

/*

  • decode_mime()转换邮件标题的字符编码,处理乱码
    */
    function decode_mime($str){
    $str=imap_mime_header_decode($str);
    return $str[0]->text;
    echo "str";print_r($str);
    if ($str[0]->charset!="default")
    {echo "==".$str[0]->text;
    return iconv($str[0]->charset,$str[0]->text);
    }
    else
    {
    return $str[0]->text;
    }
    }

/**

  • Set path name of the uploaded file to be saved.
  • @param int $fileID
  • @param string $extension
  • @access public
  • @return string
    */
    public function setPathName($fileID,$extension)
    {
    return date('Ym/dHis',time()) . $fileID . mt_rand(0,10000) . '.' . $extension;
    }

}
?>

3.控制层

./mailreceived/mailControl.php

./mailreceived/mailControl.php 内容如下:

public function __construct()
{
$this->now = date("Y-m-d H:i:s",time());

$this->setSavePath(); 

}

/**

  • mail Received()读取收件箱邮件
  • @param
  • @access public
  • @return result
    */
    public function mailReceived()
    {
    // Creating a object of reciveMail Class
    $obj= new receivemail($this->mailAccount,$this->mailPasswd,$this->mailAddress,$this->mailServer,$this->serverType,$this->port,false);
//Connect to the Mail Box 
$res=$obj->connect();     //If connection fails give error message and exit 
if (!$res) 
{ 
  return array("msg"=>"Error: Connecting to mail server"); 
} 
// Get Total Number of Unread Email in mail box 
$tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value 
if($tot < 1) { //如果信件数为0,显示信息 
  return array("msg"=>"No Message for ".$this->mailAccount); 
} 
else 
{ 
  $res=array("msg"=>"Total Mails:: $tot<br>"); 

  for($i=$tot;$i>0;$i--) 
  { 
    $head=$obj->getHeaders($i); // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName) 

    //处理邮件附件 
    $files=$obj->GetAttach($i,$this->savePath); // 获取邮件附件,返回的邮件附件信息数组 

    $imageList=array(); 
    foreach($files as $k => $file) 
    {       
      //type=1为附件,0为邮件内容图片 
      if($file['type'] == 0) 
      { 
        $imageList[$file['title']]=$file['pathname']; 
      } 
    } 
    $body = $obj->getBody($i,$this->webPath,$imageList); 

    $res['mail'][]=array('head'=>$head,'body'=>$body,"attachList"=>$files);        

// $obj->deleteMails($i); // Delete Mail from Mail box
// $obj->move_mails($i,"taskMail");
}
$obj->close_mailbox(); //Close Mail Box
return $res;
}
}

/**

  • creatBox
  • @access public
  • @return void
    */
    public function creatBox($boxName)
    {
    // Creating a object of reciveMail Class
    $obj= new receivemail($this->mailAccount,false);
    $obj->creat_mailbox($boxName);
    }

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读