1

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

电子邮件在回复时的结构方式有什么不同吗?我需要一种阅读电子邮件中内容的方法,以便使用内容更新票证。知道要更新哪封电子邮件只是纯粹阅读我正在努力的内容。

这是代码

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();

希望这是有道理的,有人可以真正提供帮助。

非常感谢提前

乔恩

4

1 回答 1

2

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

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

意味着它只查看正文 1。如果是纯文本电子邮件正文 1 将是正确的,但如果是multipart/alternative(文本和 html)正文 1 将是基本MIME消息,它告诉您有子正文(正文 1.1、1.2)实际包含内容。

此外,如果回复包含嵌入的图像,或者包含作为附件回复的消息,那么您可能会有更多的正文/位置。

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

于 2012-10-15T14:46:08.973 回答