3

我正在尝试使用 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

所以我不知道那是什么意思。我用谷歌搜索,所有结果似乎都表明这是一个“神秘错误”。看起来它即将第三次输出正文(我真的不知道那个奇怪的字符串是什么......也许是一个电子邮件 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();

这为我摆脱了错误通知。

这是我在 Kohana 讨论板上找到我的解决方案的地方

4

0 回答 0