尝试使用PHP解析IMAP电子邮件>致命错误:在第0行的“未知”中没有堆栈帧时抛出异常

时间:2013-01-19 02:05:22

标签: php email

我正在尝试使用PHP解析发送到“parsethis@mysite.com”的电子邮件(我将使用cronjob,但现在我只是在我的浏览器中点击mysite.com/cronJobs/parseMail)。

这是我第一次尝试解析电子邮件..所以我只是不确定如何解决这个问题。

这是我正在使用的代码,在网站上找到它,它似乎是我可以使用的东西。 (是的,我已经替换了所有占位符)

        $mailbox = imap_open("{mysite.com:143/notls}INBOX", "parsethis@mysite.com", "123password");  //connects to mailbox on your server

        if ($mailbox == false) {
            echo "<p>Error: Can't open mailbox!</p>";
            echo imap_last_error();
        }else{

            //Check number of messages
            $num = imap_num_msg($mailbox);

            //if there is a message in your inbox
            if( $num > 0 ) { //this just reads the most recent email. In order to go through all the emails, you'll have to loop through the number of messages
                $email = imap_fetchheader($mailbox, $num); //get email header

                $lines = explode("\n", $email);

                // data we are looking for
                $from = "";
                $subject = "";
                $to = "";
                $headers = "";
                $splittingheaders = true;

                for ($i=0; $i < count($lines); $i++) {
                    if ($splittingheaders) {
                    // this is a header
                    $headers .= $lines[$i]."\n";

                    // look out for special headers
                        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
                            $subject = $matches[1];
                        }
                        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
                            $from = $matches[1];
                        }
                        if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
                            $to = $matches[1];
                        }
                    }
                }
                //We can just display the relevant information in our browser, like below or write some method, that will put that information in a database
                echo "FROM: ".$from."<br>";
                echo "TO: ".$to."<br>";
                echo "SUBJECT: ".$subject."<br>";
                echo "BODY: ".imap_qprint(imap_body($mailbox, $num));

                //delete message
                // imap_delete($mailbox,$num);  // not while testing
                // imap_expunge($mailbox);  // not while testing
            }else{
                // echo "No more messages";
            }
        imap_close($mailbox);
    }

问题是:当我点击它时我会得到这个

FROM: "K.K.Smith" 
TO: parsethis@mysite.com 
SUBJECT: test subject 
BODY: --50f9f846_140e0f76_3df1 Content-Type: // etc .. with the body of the email unformatted in a continuous string

// INTERESTING > I get the body twice .. doubled.. once as a long unformatted string, and then again with formatting
--50f9f846_140e0f76_3df1 Content-Type: text/html; // etc.. the rest of the body with formatting .. ie my signature is correctly formatted with spacing and line breaks unlike the first body output

/// ***and then this weird error at the end*** 
--50f9f846_140e0f76_3df1-- 
Fatal error: Exception thrown without a stack frame in Unknown on line 0

所以我不知道这意味着什么。我用Google搜索,所有结果似乎表明这是一个“神秘的错误”。它看起来好像是第三次输出身体(我真的不知道那个奇怪的字符串是什么......可能是一个电子邮件ID?)..然后它窒息了它之前没有窒息的两次。

..任何人都可以提出我应该如何前进的想法吗?

修改

所以我将parseMail函数减少到最小..

public function parseMail(){
    $mailbox = imap_open("{mysite.com:143/notls}INBOX", "parsethis@mysite.com", "123password");  //connects to mailbox on your server
    imap_close($mailbox);
}

当我点击它时我仍然收到错误。想法?

解决

看起来它与Codeigniter将IMAP函数抛出的错误/警告解释为异常有关。

我的解决方案是将它放在我的imap_close()函数

之后
imap_errors();

这摆脱了我的错误通知。

Here is where i found my solution on the Kohana discussion board

0 个答案:

没有答案