marubox = imap_open($mailLink,$mailUser,$mailPass); if (!$this->marubox) { echo "Error: Connecting to mail server"; exit; } return true; } /** * 获取邮件数 * @return false|int */ function getTotalMails() { if (!$this->marubox){ return false; } $date = date("j F Y"); $emails = imap_search($this->marubox,'SINCE "'.$date.'"'); return $emails; } /** * 获取邮件头部信息 * @param $mid * @return array|false */ function getHeaders($mid) // Get Header info { if (!$this->marubox){ return false; } $mail_header = imap_header($this->marubox, $mid); $sender = $mail_header->from[0]; if (strtolower($sender->mailbox) != 'mailer-daemon' && strtolower($sender->mailbox) != 'postmaster') { $subject = $this->decode_mime($mail_header->subject); $mail_details = array( 'emailId'=>$mid, 'from' => strtolower($sender->mailbox) . '@' . $sender->host, 'subject'=>$subject,//imap_mime_header_decode($mail_header->subject)[0]->text, 'sendTime' => $mail_header->udate, 'isReply'=>0 ); $reply = substr($mail_details['subject'],0,2); if($reply == 'RE' || $reply == 'Re' ){ $mail_details['isReply'] = 1; } } return $mail_details; } /** * 处理邮件附件 * @param $mid * @return array|false */ function getAttach($mid) // 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($mid, $key, $file['extension']); $file['title'] = !empty($name) ? htmlspecialchars($name) : str_replace('.' . $file['extension'], '', $name); if (@$struckture->parts[$key]->disposition == "attachment") { // 是附件 $path = ROOT_PATH.'public' . DS . 'downAttachment' . DS . date('Ymd') .DS; $file['type'] = 1; } else { // 是正文内容 $file['type'] = 0; $path = ROOT_PATH.'public' . DS . 'contentImg' . DS . date('Ymd') .DS; } $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; } if (!file_exists($path)){ mkdir($path,0777,true); } $fp = fopen($path . $file['pathname'], "w+"); fwrite($fp, $message); fclose($fp); $file['url']=$path . $file['pathname']; $files[] = $file; } // 处理内容中包含图片的部分 if($key == 0){ if (isset($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[0]->parts[$keyb]->dparameters[0]->value); $extend = explode(".", $name); $file['extension'] = $extend[count($extend) - 1]; $file['pathname'] = $this->setPathName($mid,$key, $file['extension'],0); $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; $partnro = ($key + 1) . "." . ($keyb + 1); $message = imap_fetchbody($this->marubox, $mid, $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; } $path = ROOT_PATH.'public' . DS . 'contentImg' . DS . date('Ymd') .DS; if (!file_exists($path)){ mkdir($path,0777,true); } $fp = fopen($path . $file['pathname'], "w+"); fwrite($fp, $message); fclose($fp); $file['url']=$path . $file['pathname']; $files[] = $file; } } } } } } return $files; } /** * 获取正文 * @param $mid * @param $path * @param $imageList * @return false|mixed|string|string[]|void */ function getBody($mid,$path,$imageList,$section) // Get Message Body { if (!$this->marubox){ return false; } $struckture = imap_fetchstructure($this->marubox, $mid); if(isset($struckture->parts)) { if($struckture->subtype == 'MIXED' || $struckture->subtype == 'RELATED' ){ $encoding= $struckture->parts[0]->parts[0]->encoding; } if($struckture->subtype == 'ALTERNATIVE'){ $encoding = $struckture->parts[0]->encoding; } $body = $this->contentDecoder($encoding,$mid,$section); //处理图片 $body = $this->embed_images($body, $path, $imageList); return $body; } } private function contentDecoder($encoding, $num,$section) //$this->mbox, $num, $section { switch ($encoding) { case 0: case 1: $message = imap_8bit(imap_fetchbody($this->marubox, $num, $section)); break; case 2: $message = imap_binary(imap_fetchbody($this->marubox, $num, $section)); break; case 3: $message = iconv('gbk', 'utf-8',imap_base64(imap_fetchbody($this->marubox, $num, $section))); break; case 4: $message = quoted_printable_decode(imap_fetchbody($this->marubox, $num, $section)); break; } return $message; } function embed_images(&$body,&$path,$imageList) { // get all img tags preg_match_all('//', $body, $matches); if (!isset($matches[0])) return; foreach ($matches[0] as $key=>$img) { // halt($path.$imageList[$key]); // 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, '', $body); // $body = str_replace($img, '', $body); } } return $body; } 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, $msg_number, $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', 'utf8', $text); } } if ($structure->type == 1) /* multipart */ { while (list($index, $sub_structure) = $this->new_each($structure->parts)) { if ($part_number) { $prefix = $part_number . '.'; }else{ $prefix = "1"; } $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); if ($data) { return $data; } } } } return false; } 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 new_each(&$array){ $res = array(); $key = key($array); if($key !== null){ next($array); $res[1] = $res['value'] = $array[$key]; $res[0] = $res['key'] = $key; }else{ $res = false; } return $res; } /** * 标题编码转化 * @param $str * @return false|string */ function decode_mime($str) { $str = imap_mime_header_decode($str); return $str[0]->text; if ($str[0]->charset != "default") { return iconv($str[0]->charset, 'utf8', $str[0]->text); } else { return $str[0]->text; } } /** * Set path name of the uploaded file to be saved. * * @param int $mid * @param int $fileID * @param string $extension * @access public * @return string */ public function setPathName($mid, $fileID, $extension,$type = 1) { if($type == 1){ return $mid."-".$fileID .'.' . $extension; }else{ return $mid."-".$fileID .mt_rand(0, 10000) .'.' . $extension; } } }