PHP Imap阅读收件箱中的回复

时间:2012-10-15 14:33:31

标签: php imap

我已经编写/复制了一个脚本,该脚本从收件箱中读取电子邮件并更新票证,然后将电子邮件移动到proccessed文件夹。这一切都完美适用于收件箱中的新电子邮件,但当有人回复电子邮件并最终收件箱时,我的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();

希望有所帮助,有人可以提供帮助。

许多人提前感谢

乔恩

1 个答案:

答案 0 :(得分:2)

最明显的是你的代码假设一条消息总是在第1部分,你的行:

'body'      => $this->cleanBody(imap_fetchbody($this->conn, $i,1)),

表示它仅查看正文1.如果它是纯文本电子邮件正文1将是正确的,但如果它是multipart/alternative(text&amp; html)正文1将是基本MIME消息告诉你有子实体(主体1.1,1.2)实际上包含内容。

此外,如果回复包含嵌入的图片,或者包含它作为附件回复的邮件,那么您可以拥有更多的主体/位置。

那我如何找到身体?(你问)......你可以用imap_fetchstructure来了解所有的身体部位,然后搜索它以找到一块使用type = 0(文本)然后下载该正文部分。 (第一个找到的文本应该是正确的,但请注意,电子邮件中可能有多个文本正文类型。)