加入收藏 | 设为首页 | 会员中心 | 我要投稿 厦门网 (https://www.xiamenwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

PHP receiveMail实现收邮件功能

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

function GetAttach($mid,$path) // Get Atteced File from Mail
{
if(!$this->marubox)
return false;

$struckture = imap_fetchstructure($this->marubox,$mid); 

$files=array(); 
if($struckture->parts) 
{ 
  foreach($struckture->parts as $key => $value) 
  { 
    $enc=$struckture->parts[$key]->encoding; 

    //取邮件附件 
    if($struckture->parts[$key]->ifdparameters) 
    { 
      //命名附件,转码 
      $name=$this->decode_mime($struckture->parts[$key]->dparameters[0]->value);          
      $extend =explode(".",$name); 
      $file['extension'] = $extend[count($extend)-1]; 
      $file['pathname'] = $this->setPathName($key,$file['extension']); 
      $file['title']   = !empty($name) ? htmlspecialchars($name) : str_replace('.' . $file['extension'],'',$name); 
      $file['size']   = $struckture->parts[$key]->dparameters[1]->value; 

// $file['tmpname'] = $struckture->parts[$key]->dparameters[0]->value;
if(@$struckture->parts[$key]->disposition=="ATTACHMENT")
{
$file['type'] = 1;
}
else
{
$file['type'] = 0;
}
$files[] = $file;

      $message = imap_fetchbody($this->marubox,$mid,$key+1); 
      if ($enc == 0) 
        $message = imap_8bit($message); 
      if ($enc == 1) 
        $message = imap_8bit ($message); 
      if ($enc == 2) 
        $message = imap_binary ($message); 
      if ($enc == 3)//图片 
        $message = imap_base64 ($message);  
      if ($enc == 4) 
        $message = quoted_printable_decode($message); 
      if ($enc == 5) 
        $message = $message; 
      $fp=fopen($path.$file['pathname'],"w"); 
      fwrite($fp,$message); 
      fclose($fp); 

    } 
    // 处理内容中包含图片的部分 
    if($struckture->parts[$key]->parts) 
    { 
      foreach($struckture->parts[$key]->parts as $keyb => $valueb) 
      { 
        $enc=$struckture->parts[$key]->parts[$keyb]->encoding; 
        if($struckture->parts[$key]->parts[$keyb]->ifdparameters) 
        { 
          //命名图片 
          $name=$this->decode_mime($struckture->parts[$key]->parts[$keyb]->dparameters[0]->value); 
          $extend =explode(".",$name); 
          $file['extension'] = $extend[count($extend)-1]; 
          $file['pathname'] = $this->setPathName($key,$file['extension']); 
          $file['title']   = !empty($name) ? htmlspecialchars($name) : str_replace('.' . $file['extension'],$name); 
          $file['size']   = $struckture->parts[$key]->parts[$keyb]->dparameters[1]->value; 

// $file['tmpname'] = $struckture->parts[$key]->dparameters[0]->value;
$file['type'] = 0;
$files[] = $file;

          $partnro = ($key+1).".".($keyb+1); 

          $message = imap_fetchbody($this->marubox,$partnro); 
          if ($enc == 0) 
              $message = imap_8bit($message); 
          if ($enc == 1) 
              $message = imap_8bit ($message); 
          if ($enc == 2) 
              $message = imap_binary ($message); 
          if ($enc == 3) 
              $message = imap_base64 ($message); 
          if ($enc == 4) 
              $message = quoted_printable_decode($message); 
          if ($enc == 5) 
              $message = $message; 
          $fp=fopen($path.$file['pathname'],"w"); 
          fwrite($fp,$message); 
          fclose($fp); 
        } 
      } 
    }         
  } 
} 
//move mail to taskMailBox 
$this->move_mails($mid,$this->marubox);    

return $files; 

}

function getBody($mid,&$path,$imageList) // Get Message Body
{
if(!$this->marubox)
return false;

$body = $this->get_part($this->marubox,"TEXT/HTML"); 
if ($body == "") 
  $body = $this->get_part($this->marubox,"TEXT/PLAIN"); 
if ($body == "") {  
  return ""; 
} 
//处理图片 
$body=$this->embed_images($body,$path,$imageList); 
return $body; 

}

function embed_images(&$body,$imageList)
{
// get all img tags
preg_match_all('/<img.*?>/',$body,$matches);
if (!isset($matches[0])) return;

foreach ($matches[0] as $img) 
{ 
  // replace image web path with local path 
  preg_match('/src="(.*?)"/',$img,$m); 
  if (!isset($m[1])) continue; 
  $arr = parse_url($m[1]); 
  if (!isset($arr['scheme']) || !isset($arr['path']))continue; 

// if (!isset($arr['host']) || !isset($arr['path']))continue;
if ($arr['scheme']!="http")
{
$filename=explode("@",$arr['path']);
$body = str_replace($img,'PHP receiveMail实现收邮件功能',$body);
}
}
return $body;
}

function deleteMails($mid) // Delete That Mail
{
if(!$this->marubox)
return false;

imap_delete($this->marubox,$mid); 

}
function close_mailbox() //Close Mail Box
{
if(!$this->marubox)
return false;

imap_close($this->marubox,CL_EXPUNGE); 

}

(编辑:厦门网)

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

热点阅读