收件箱中的PHP Imap阅读回复

收件箱中的PHP Imap阅读回复,php,imap,Php,Imap,我编写/复制了一个脚本,该脚本从收件箱中读取电子邮件并更新票证,然后将电子邮件移动到已处理的文件夹中。这一切在收件箱中的新邮件上都非常有效,但是当有人回复一封邮件时,它最终进入了收件箱,我的scrtipt在邮件上什么也看不到 回复邮件时,邮件的结构有什么不同吗?我需要一种阅读电子邮件内容的方法,这样我就可以用内容更新机票。知道要更新哪封电子邮件,这一切都由我来处理,它只是单纯地阅读我正在努力处理的内容 这是密码 class Email { // imap server connect

我编写/复制了一个脚本,该脚本从收件箱中读取电子邮件并更新票证,然后将电子邮件移动到已处理的文件夹中。这一切在收件箱中的新邮件上都非常有效,但是当有人回复一封邮件时,它最终进入了收件箱,我的scrtipt在邮件上什么也看不到

回复邮件时,邮件的结构有什么不同吗?我需要一种阅读电子邮件内容的方法,这样我就可以用内容更新机票。知道要更新哪封电子邮件,这一切都由我来处理,它只是单纯地阅读我正在努力处理的内容

这是密码

class Email 
{

    // imap server connection
    public $conn;

    // inbox storage and inbox message count
    public $inbox;
    private $msg_cnt;

    // email login credentials

        private $server = '????????????';

    private $user   = '????????';
    private $pass   = '?????????????';
    private $port   = ??;

    // connect to the server and get the inbox emails
    function __construct() 
        {
        $this->connect();
        $this->inbox();

        }

        function getdecodevalue($message,$coding) 
        {
            switch($coding) {
                    case 0:
                    case 1:
                            $message = imap_8bit($message);
                            break;
                    case 2:
                            $message = imap_binary($message);
                            break;
                    case 3:
                    case 5:
                            $message=imap_base64($message);
                            break;
                    case 4:
                            $message = imap_qprint($message);
                            break;
            }
            return $message;
        }

    // close the server connection
    function close() 
        {
        $this->inbox = array();
        $this->msg_cnt = 0;

        imap_close($this->conn);
    }

    // open the server connection
    // the imap_open function parameters will need to be changed for the particular server
    // these are laid out to connect to a Dreamhost IMAP server
    function connect() 
        {
        $this->conn = imap_open("{".$this->server.":".$this->port."/imap/novalidate-cert}INBOX", $this->user, $this->pass);

    }

    // move the message to a new folder
    function move($msg_index, $folder='Read') 
        {
        // move on server
            imap_mail_move($this->conn, $msg_index, $folder);


        // re-read the inbox
        //$this->inbox();
    }

    // get a specific message (1 = first email, 2 = second email, etc.)
    function get($msg_index=NULL) 
        {
        if(count($this->inbox) <= 0) 
    {
            return array();
        }
        elseif( ! is_null($msg_index) && isset($this->inbox[$msg_index])) 
    {
            return $this->inbox[$msg_index];
        }

        return $this->inbox[0];
    }

    // read the inbox
    function inbox() 
        {
        $this->msg_cnt = imap_num_msg($this->conn);

        $in = array();
        for($i = 1; $i <= $this->msg_cnt; $i++) 
    {
                $in[] = array(
                        'index'     => $i,
                        'header'    => imap_headerinfo($this->conn, $i),
                        'body'      => $this->cleanBody(imap_fetchbody($this->conn, $i,1)),
                        'structure' => imap_fetchstructure($this->conn, $i)
                    );
        }

        $this->inbox = $in;
    }


        function cleanBody($body) 
        {


            $delimiter = '#';
            $startTag = '----------START REPLY----------';
            $endTag = '----------END REPLY----------';
            $regex = $delimiter . preg_quote($startTag, $delimiter) 
                                . '(.*?)' 
                                . preg_quote($endTag, $delimiter) 
                                . $delimiter 
                                . 's';
            preg_match($regex,$body,$matches);

            $ret = trim($matches[1]);

            return $ret;
    }

}




$emails = new Email();



$email = array();
$emailCount = 1;
foreach($emails->inbox as $ems => $em)
{

            $email[$emailCount]['subject'] = $sub = $em['header']->subject;

            //echo $sub;

            $subParts = explode('-',$sub);

            $ticketUniqueCode = trim($subParts[1]);

            $sql = "SELECT * FROM ticket_main WHERE uniquecode = '".mysql_escape_string($ticketUniqueCode)."' LIMIT 1";

            $query = mysql_query($sql);

            if(mysql_num_rows($query))
            {
                $res = mysql_fetch_object($query);

                $ticketBody = $em['body'];
                $customerID = $res->customerID;


                $sql2 = "INSERT INTO ticket_updates SET ticketID = '".$res->ticketID."' , submitted = NOW() , submittedBy = '".$res->customerID."' , message = '".mysql_escape_string($ticketBody)."' , inhouse = 0";
                $query = mysql_query($sql2);


                // attachment section
                $message_number = $em['index'];

                $attachments = array();
                if(isset($em['structure']->parts) && count($em['structure']->parts)) 
                {
                        //echo 'hi';
                        for($i = 0; $i < count($em['structure']->parts); $i++) 
                        {
                                $attachments[$i] = array(
                                        'is_attachment' => false,
                                        'filename' => '',
                                        'name' => '',
                                        'attachment' => ''
                                );

                                if($em['structure']->parts[$i]->ifdparameters) {
                                               foreach($em['structure']->parts[$i]->dparameters as $object) 
                                        {
                                                if(strtolower($object->attribute) == 'filename') 
                                                {
                                                        $attachments[$i]['is_attachment'] = true;
                                                        $attachments[$i]['filename'] = $object->value;
                                                }
                                        }
                                }

                                if($em['structure']->parts[$i]->ifparameters) {
                                        foreach($em['structure']->parts[$i]->parameters as $object) 
                                        {
                                                if(strtolower($object->attribute) == 'name') 
                                                {
                                                        $attachments[$i]['is_attachment'] = true;
                                                        $attachments[$i]['name'] = $object->value;
                                                }
                                        }
                                }
                                if($attachments[$i]['is_attachment']) 
                                {
                                        $attachments[$i]['attachment'] = imap_fetchbody($emails->conn, $message_number, $i+1);
                                        if($em['structure']->parts[$i]->encoding == 3) 
                                        { // 3 = BASE64
                                                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                                        }
                                        elseif($em['structure']->parts[$i]->encoding == 4) 
                                        { // 4 = QUOTED-PRINTABLE
                                                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                                        }
                                }

                                if(isset($em['structure']->parts[$i]->disposition) && $em['structure']->parts[$i]->disposition == "attachment") 
                                {
                                        $filename = $attachments[$i]['name'];
                                        $mege="";
                                        $data="";
                                        $mege = imap_fetchbody($emails->conn, $message_number, $i+1);
                                        $filename= $filename;
                                        $fp=fopen('???????????????'.$filename,"w");
                                        $data=$emails->getdecodevalue($mege,$em['structure']->parts[$i]->type);

                                        fputs($fp,$data);
                                        fclose($fp);
                                        $email[$emailCount]['attachment'] = $attachments;



                                }


                        }


                }

            }

            $emailCount++;

}
$emailNumbers = imap_search($emails->conn,'ALL');

if(!empty($emailNumbers))
{
foreach($emailNumbers as $a)
{
    $emails->move($a);
}
imap_expunge($emails->conn);
}
$emails->close();
class电子邮件
{
//imap服务器连接
公共康涅狄格州;
//收件箱存储和收件箱邮件计数
公共邮箱;
私人$msg_cnt;
//电子邮件登录凭据
private$server='???????';
private$user='???';
私人$pass='???????';
私有$port=??;
//连接到服务器并获取收件箱电子邮件
函数_u构造()
{
$this->connect();
$this->inbox();
}
函数getdecodevalue($message,$coding)
{
交换机($编码){
案例0:
案例1:
$message=IMAP8bit($message);
打破
案例2:
$message=imap_二进制($message);
打破
案例3:
案例5:
$message=imap_base64($message);
打破
案例4:
$message=imap_qprint($message);
打破
}
返回$message;
}
//关闭服务器连接
函数关闭()
{
$this->inbox=array();
$this->msg_cnt=0;
imap_关闭($this->conn);
}
//打开服务器连接
//需要为特定服务器更改imap_open功能参数
//这些被布置成连接到Dreamhost IMAP服务器
函数连接()
{
$this->conn=imap_open(“{”。$this->server.”:“$this->port.”/imap/novalidate cert}收件箱,$this->user,$this->pass);
}
//将邮件移动到新文件夹
函数移动($msg\u index,$folder='Read')
{
//在服务器上移动
imap_邮件移动($this->conn,$msg_index,$folder);
//重新阅读收件箱
//$this->inbox();
}
//获取特定消息(1=第一封电子邮件,2=第二封电子邮件,等等)
函数get($msg_index=NULL)
{
如果(计数($this->inbox)收件箱[$msg_index]))
{
返回$this->inbox[$msg_index];
}
返回$this->inbox[0];
}
//阅读收件箱
函数收件箱()
{
$this->msg\u cnt=imap\u num\u msg($this->conn);
$in=array();
对于($i=1;$i msg\u cnt;$i++)
{
[]=数组中的$(
“索引”=>$i,
'header'=>imap_headerinfo($this->conn$i),
“body”=>$this->cleanBody(imap_fetchbody($this->conn$i,1)),
'structure'=>imap_fetchstructure($this->conn$i)
);
}
$this->inbox=$in;
}
函数cleanBody($body)
{
$delimiter='#';
$startTag='----开始回复-----';
$endTag='----结束回复-----';
$regex=$delimiter.preg_quote($startTag,$delimiter)
. '(.*?)' 
.preg_quote($endTag,$delimiter)
.$分隔符
"s",;
preg_match($regex,$body,$matches);
$ret=修剪($matches[1]);
返回$ret;
}
}
$emails=新电子邮件();
$email=array();
$emailCount=1;
foreach($emails->收件箱为$ems=>$em)
{
$email[$emailCount]['subject']=$sub=$em['header']->subject;
//echo$sub;
$subParts=分解('-',$subpart);
$ticketUniqueCode=trim($subParts[1]);
$sql=“选择*从票证\主票证,其中uniquecode=”。mysql\转义\字符串($ticketUniqueCode)。“'LIMIT 1';
$query=mysql\u查询($sql);
if(mysql_num_行($query))
{
$res=mysql\u fetch\u对象($query);
$ticketBody=$em['body'];
$customerID=$res->customerID;
$sql2=“INSERT INTO ticket\u updates SET ticketID=”””$res->ticketID.”,submittedBy=”,$res->customerID.”,message=”,mysql\u escape\u string($ticketBody)。“,inhouse=0”;
$query=mysql\u查询($sql2);
//附件部分
$message_number=$em['index'];
$attachments=array();
if(isset($em['structure']->部分)和计数($em['structure']->部分))
{
//回音“嗨”;
对于($i=0;$iparts);$i++)
{
$attachments[$i]=数组(
'is_attachment'=>false,
'文件名'=>'',
'名称'=>'',
“附件”=>“
);
if($em['structure']->parts[$i]->ifdparameters){
foreach($em['structure']->parts[$i]->dparameters as$object)
{
如果(strtolower($object->attribute)='filename')
{
'body'      => $this->cleanBody(imap_fetchbody($this->conn, $i,1)),