php imap函数 获取邮件信息

时间 15 : 12 , Aug 05,2019

用的tp5.0框架

<?php
/**
*Email imap类
*/

namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Session;
use think\Exception;
use think\Request;

class Email extends Controller{

    private $_connect;
    private $_mailInfo;
    private $_totalCount;
    private $_contentType;

  public function __construct(){
        $this->_contentType = array(
            'ez' => 'application/andrew-inset', 'hqx' => 'application/mac-binhex40',
            'cpt' => 'application/mac-compactpro', 'doc' => 'application/msword',
            'bin' => 'application/octet-stream', 'dms' => 'application/octet-stream',
            'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream',
            'exe' => 'application/octet-stream', 'class' => 'application/octet-stream',
            'so' => 'application/octet-stream', 'dll' => 'application/octet-stream',
            'oda' => 'application/oda', 'pdf' => 'application/pdf',
            'ai' => 'application/postscript', 'eps' => 'application/postscript',
            'ps' => 'application/postscript', 'smi' => 'application/smil',
            'smil' => 'application/smil', 'mif' => 'application/vnd.mif',
            'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint',
            'wbxml' => 'application/vnd.wap.wbxml', 'wmlc' => 'application/vnd.wap.wmlc',
            'wmlsc' => 'application/vnd.wap.wmlscriptc', 'bcpio' => 'application/x-bcpio',
            'vcd' => 'application/x-cdlink', 'pgn' => 'application/x-chess-pgn',
            'cpio' => 'application/x-cpio', 'csh' => 'application/x-csh',
            'dcr' => 'application/x-director', 'dir' => 'application/x-director',
            'dxr' => 'application/x-director', 'dvi' => 'application/x-dvi',
            'spl' => 'application/x-futuresplash', 'gtar' => 'application/x-gtar',
            'hdf' => 'application/x-hdf', 'js' => 'application/x-javascript',
            'skp' => 'application/x-koan', 'skd' => 'application/x-koan',
            'skt' => 'application/x-koan', 'skm' => 'application/x-koan',
            'latex' => 'application/x-latex', 'nc' => 'application/x-netcdf',
            'cdf' => 'application/x-netcdf', 'sh' => 'application/x-sh',
            'shar' => 'application/x-shar', 'swf' => 'application/x-shockwave-flash',
            'sit' => 'application/x-stuffit', 'sv4cpio' => 'application/x-sv4cpio',
            'sv4crc' => 'application/x-sv4crc', 'tar' => 'application/x-tar',
            'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
            'texinfo' => 'application/x-texinfo', 'texi' => 'application/x-texinfo',
            't' => 'application/x-troff', 'tr' => 'application/x-troff',
            'roff' => 'application/x-troff', 'man' => 'application/x-troff-man',
            'me' => 'application/x-troff-me', 'ms' => 'application/x-troff-ms',
            'ustar' => 'application/x-ustar', 'src' => 'application/x-wais-source',
            'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml',
            'zip' => 'application/zip', 'au' => 'audio/basic', 'snd' => 'audio/basic',
            'mid' => 'audio/midi', 'midi' => 'audio/midi', 'kar' => 'audio/midi',
            'mpga' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg',
            'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
            'm3u' => 'audio/x-mpegurl', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
            'rpm' => 'audio/x-pn-realaudio-plugin', 'ra' => 'audio/x-realaudio',
            'wav' => 'audio/x-wav', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-xyz',
            'bmp' => 'image/bmp', 'gif' => 'image/gif', 'ief' => 'image/ief',
            'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'jpe' => 'image/jpeg',
            'png' => 'image/png', 'tiff' => 'image/tiff', 'tif' => 'image/tiff',
            'djvu' => 'image/vnd.djvu', 'djv' => 'image/vnd.djvu', 'wbmp' => 'image/vnd.wap.wbmp',
            'ras' => 'image/x-cmu-raster', 'pnm' => 'image/x-portable-anymap',
            'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap',
            'ppm' => 'image/x-portable-pixmap', 'rgb' => 'image/x-rgb', 'xbm' => 'image/x-xbitmap',
            'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'igs' => 'model/iges',
            'iges' => 'model/iges', 'msh' => 'model/mesh', 'mesh' => 'model/mesh',
            'silo' => 'model/mesh', 'wrl' => 'model/vrml', 'vrml' => 'model/vrml',
            'css' => 'text/css', 'html' => 'text/html', 'htm' => 'text/html',
            'asc' => 'text/plain', 'txt' => 'text/plain', 'rtx' => 'text/richtext',
            'rtf' => 'text/rtf', 'sgml' => 'text/sgml', 'sgm' => 'text/sgml',
            'tsv' => 'text/tab-separated-values', 'wml' => 'text/vnd.wap.wml',
            'wmls' => 'text/vnd.wap.wmlscript', 'etx' => 'text/x-setext',
            'xsl' => 'text/xml', 'xml' => 'text/xml', 'mpeg' => 'video/mpeg',
            'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'qt' => 'video/quicktime',
            'mov' => 'video/quicktime', 'mxu' => 'video/vnd.mpegurl', 'avi' => 'video/x-msvideo',
            'movie' => 'video/x-sgi-movie', 'ice' => 'x-conference/x-cooltalk',
            'rar' => 'application/x-rar-compressed', 'zip' => 'application/x-zip-compressed',
            '*' => 'application/octet-stream', 'docx' => 'application/msword',
        );
    }

    /**
    * 链接邮箱服务器
    */
    public function mailConnect($host, $port, $user, $pass) {
        $this->_connect = imap_open("{" . "$host:$port" . "}INBOX", $user, $pass);
        if(!$this->_connect){
            die('链接错误,账号密码不正确');
            return false;
        }
        return $this->_connect;
    }

    /**
     * 获取邮件信息
     */
    public function mailInfo() {
        $this->_mailInfo = imap_mailboxmsginfo($this->_connect);
        if (!$this->_mailInfo) {
            echo "get mailInfo failed: " . imap_last_error();
            return false;
        }
        return $this->_mailInfo;
    }

    /**
    * Read an overview of the information in the headers of the given message  阅读给定消息头中的信息概述,参数可以是“1,2,3,.....”
    */
    public function mailList($msgRange = '') {
        if ($msgRange) {
            $range = $msgRange;
        } else {
            $this->mailTotalCount();
            $range = "1:" . $this->_totalCount;
        }
        $overview = imap_fetch_overview($this->_connect, $range);
        foreach ($overview as $val) {
            $mailList[$val->msgno] = (array) $val;
        }
        return $mailList;
    }

    /**
    * 获取邮件总数
    */
    public function mailTotalCount(){
        $check = imap_check($this->_connect);
        $this->_totalCount = $check->Nmsgs;
        return $this->_totalCount;
    }

    /**
    * Read the header of the message 阅读邮件的标题
    */
    public function mailHeader($msgCount){
        $mailHeader = array();-
        $header = imap_header($this->_connect, $msgCount);
        $sender = $header->from[0];
        $replyTo = $header->reply_to[0];
        if(strtolower($sender->mailbox) != 'mailer-daemon' && strtolower($sender->mailbox) != 'postmaster'){
            $subject = $this->subjectDecode($header->subject);
            $to = $this->subjectDecode($header->toaddress);
            $toOther = $this->subjectDecode($replyTo->mailbox);
            $mailHeader = array(
                'from' => strtolower($sender->mailbox) . '@' . $sender->host,
                'fromName' => $this->subjectDecode($sender->personal),
                'toOther' => strtolower($toOther) . '@' . $replyTo->host,
                'toOtherName' => $this->subjectDecode($replyTo->personal),
                'subject' => $subject,
                'to' => strtolower($to),
                'date' => $header->date,
                'id' => $header->Msgno,
                'seen' => $header->Unseen,
            );
        }
        return $mailHeader;
    }

    /**
    * decode the subject of chinese  解码中文主题
    */
    public function subjectDecode($subject){
        $beginStr = substr($subject, 0, 5);
        if($beginStr == '=?ISO'){
            $separator = '=?ISO-2022-JP';
            $toEncoding = 'ISO-2022-JP';
        }else{
			//$separator = '=?GB2312';
			//$toEncoding = 'GB2312';
            $separator = '=?GBK';
            $toEncoding = 'GBK';
        }
        $encode = strstr($subject, $separator);
        if($encode){
            $explodeArr = explode($separator, $subject);
            $length = count($explodeArr);
            $subjectArr = array();
            for ($i = 0; $i < $length / 2; $i++) {
                $subjectArr[$i][] = $explodeArr[$i * 2];
                if (@$explodeArr[$i * 2 + 1]) {
                    $subjectArr[$i][] = $explodeArr[$i * 2 + 1];
                }
            }
            foreach ($subjectArr as $arr) {
                $subSubject = implode($separator, $arr);
                if (count($arr) == 1) {
                    $subSubject = $separator . $subSubject;
                }
                $begin = strpos($subSubject, "=?");
                $end = strpos($subSubject, "?=");
                $beginStr = '';
                $endStr = '';
                if ($end > 0) {
                    if ($begin > 0) {
                        $beginStr = substr($subSubject, 0, $begin);
                    }
                    if ((strlen($subSubject) - $end) > 2) {
                        $endStr = substr($subSubject, $end + 2, strlen($subSubject) - $end - 2);
                    }
                    $str = substr($subSubject, 0, $end - strlen($subSubject));
                    $pos = strrpos($str, "?");
                    $str = substr($str, $pos + 1, strlen($str) - $pos);
                    $subSubject = $beginStr . imap_base64($str) . $endStr;
                    $subSubjectArr[] = iconv($toEncoding, 'utf-8', $subSubject);
                    mb_convert_encoding($subSubject, 'utf-8', 'gb2312,ISO-2022-JP,cp936');      //多字节字符串编码转换
                }
            }
            $subject = implode('', $subSubjectArr);
        }
        return $subject;
    }

    /**
    * get the body of the message 获取消息正文
    */
    public function getBody($msgCount){
        $body = $this->getPart($msgCount, "TEXT/HTML");
        if($body == ''){
            $body = $this->getPart($msgCount, "TEXT/PLAIN");
        }
        if($body == ''){
            return '';
        }
        return $body;
    }

    /**
    * Read the structure of a particular message and fetch a particular 读取特定消息的结构并获取特定消息
    */
    private function getPart($msgCount, $mimeType, $structure = false, $partNumber = false){

        if(!$structure){
            $structure = imap_fetchstructure($this->_connect, $msgCount);
        }
        if($structure){
            if($mimeType == $this->getMimeType($structure)){
                if(!$partNumber){
                    $partNumber = "1";
                }
                $fromEncoding = $structure->parameters[0]->value;
                $text = imap_fetchbody($this->_connect, $msgCount, $partNumber);
                if($structure->encoding == 3){
                    $text = imap_base64($text);
                } else if ($structure->encoding == 4){
                    $text = imap_qprint($text);
                }

                $text = mb_convert_encoding($text, 'utf-8', $fromEncoding);
                return $text;
            }

            if ($structure->type == 1) {
                while (list($index, $subStructure) = each($structure->parts)) {
                    $prefix = '';
                    if ($partNumber) {
                        $prefix = $partNumber . '.';
                    }
                    $data = $this->getPart($msgCount, $mimeType, $subStructure, $prefix . ($index + 1));
                    if ($data) {
                        return $data;
                    }
                }
            }
        }
        return false;
    }

    public function test(){
	  return 123;
	}

    /**
     * get the subtype and type of the message structure   获取消息结构的子类型和类型
     */
    private function getMimeType($structure) {

        $mimeType = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
        if ($structure->subtype) {
            return $mimeType[(int) $structure->type] . '/' . $structure->subtype;
        }
        return "TEXT/PLAIN";
    
	}


    /**
     * get attach of the message   获取邮件附件
     */
    public function getAttach($msgCount, $path) {
        $struckture = imap_fetchstructure($this->_connect, $msgCount);
        $attach = array();
        if ($struckture->parts) {
            foreach ($struckture->parts as $key => $value) {
                $encoding = $struckture->parts[$key]->encoding;
                if ($struckture->parts[$key]->ifdparameters) {
                    $name = $struckture->parts[$key]->dparameters[0]->value;
                    $message = imap_fetchbody($this->_connect, $msgCount, $key + 1);
                    if ($encoding == 0) {
                        $message = imap_8bit($message);
                    } else if ($encoding == 1) {
                        $message = imap_8bit($message);
                    } else if ($encoding == 2) {
                        $message = imap_binary($message);
                    } else if ($encoding == 3) {
                        $message = imap_base64($message);
                    } else if ($encoding == 4) {
                        $message = quoted_printable_decode($message);
                    }
                    $this->downAttach($path, $name, $message);
                    $attach[] = $name;
                }
                if ($struckture->parts[$key]->parts) {
                    foreach ($struckture->parts[$key]->parts as $keyb => $valueb) {
                        $encoding = $struckture->parts[$key]->parts[$keyb]->encoding;
                        if ($struckture->parts[$key]->parts[$keyb]->ifdparameters) {
                            $name = $struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
                            $partnro = ($key + 1) . "." . ($keyb + 1);
                            $message = imap_fetchbody($this->_connect, $msgCount, $partnro);
                            if ($encoding == 0) {
                                $message = imap_8bit($message);
                            } else if ($encoding == 1) {
                                $message = imap_8bit($message);
                            } else if ($encoding == 2) {
                                $message = imap_binary($message);
                            } else if ($encoding == 3) {
                                $message = imap_base64($message);
                            } else if ($encoding == 4) {
                                $message = quoted_printable_decode($message);
                            }
                            $this->downAttach($path, $name, $message);
                            $attach[] = $name;
                        }
                    }
                }
            }
        }
        return $attach;
    }

    /**
     * download the attach of the mail to localhost 将邮件附件下载到本地主机
     */
    public function downAttach($filePath, $name, $message) {
        if (is_dir($filePath)) {
            $fileOpen = fopen($filePath . $name, "w");
        } else {
            mkdir($filePath);
        }
        fwrite($fileOpen, $message);
        fclose($fileOpen);
    }

    /**
     * click the attach link to download the attach 单击“附件”链接下载附件
     */
    public function getAttachData($id, $filePath, $fileName){
        $nameArr = explode('.', $fileName);
        $length = count($nameArr);
        $contentType = $this->_contentType[$nameArr[$length - 1]];
        if (!$contentType) {
            $contentType = $this->_contentType['*'];
        }
        $filePath = chop($filePath);
        if ($filePath != '') {
            if(substr($filePath, strlen($filePath) - 1, strlen($filePath)) != '/') {
                $filePath .= '/';
            }
            $filePath .= $fileName;
        }else{
            $filePath = $fileName;
        }
        if (!file_exists($filePath)) {
            echo 'the file is not exsit';
            return false;
        }
        $fileSize = filesize($filePath);
        header("Content-type: " . $contentType);
        header("Accept-Range : byte ");
        header("Accept-Length: $fileSize");
        header("Content-Disposition: attachment; filename=" . $fileName);
        $fileOpen = fopen($filePath, "r");
        $bufferSize = 1024;
        $curPos = 0;
        while (!feof($fileOpen) && $fileSize - $curPos > $bufferSize) {
            $buffer = fread($fileOpen, $bufferSize);
            echo $buffer;
            $curPos += $bufferSize;
        }
        $buffer = fread($fileOpen, $fileSize - $curPos);
        echo $buffer;
        fclose($fileOpen);
        return true;
    }

    /**
     * Close an IMAP stream 关闭IMAP流
     */
    public function closeMail(){
        imap_close($this->_connect, CL_EXPUNGE);
    }

}

下面是测试

<?php
namespace app\index\controller;
use think\Request;
class Index
{

       public function index(){
		   $request = Request::instance();
		   $id=$request->get('id',1,"int");  //类别
            set_time_limit(0);
			$email=new Email;
			echo $email->test();
			$mailserver= "imap.qq.com"; //IMAP主机
			$username = "xxxxxxxx@qq.com"; //邮箱用户名
			$password = "vnasuxxxxxxtjgfa"; //邮箱pop密码

			$connect=$email->mailConnect($mailserver, '143', $username, $password);
			var_dump($connect);
			echo "<hr/>";
			$total = $email->mailTotalCount();// 邮件总数
			var_dump($total);
			echo "<hr/>";
			$allus = imap_status($connect, "{imap.qq.com:143}INBOX", SA_ALL);
			var_dump($allus);
			echo "<hr/>";
            $unseen = $allus->unseen;  //未读邮件
			var_dump($unseen);
            echo "<hr/>";

		   /* $start=$total-100;                 //数字最大的为最新邮件,取100封邮件

           for($i=$total;$i>$start;$i--){
                $mailHeader = $email->mailHeader($i);  //获取邮件头部信息 
                echo $i." . ";
				var_dump($mailHeader);
                echo "<hr/>";
		   }*/
 
           $mailInfo=$email->mailInfo();        //获取邮件信息
           var_dump($mailInfo);
           echo "<hr/>";

           $mailHeader = $email->mailHeader($id);  //获取邮件头部信息 
           var_dump($mailHeader);
           echo "<hr/>";

           $mailList=$email->mailList($id);   //阅读给定消息头中的信息概述
           var_dump($mailList);
           echo "<hr/>";

           $mailBode=$email->getBody($id);   //获取消息正文
           echo ($mailBode);
           echo "<hr/>";

		   $email->closeMail();               //关闭邮箱
	   }
}

下面是运行截图,获取的邮件是来自智联招聘的简历,可以对邮件主体内容操作,来获取想要的数据

image.png

留 言

  

网站简介

作者的个人博客,记事本,记录工作和学习中遇到的问题和一些基础功能开发!
好好学习,天天向上!

常用网站

Copyright © 2016-2024 LXC.All rights reserved. 皖ICP备2023025580号-1