Trying to parse IMAP email with PHP> Fatal error: Exception thrown without stack frame Unknown on line 0

I am trying to parse emails sent to " parsethis@mysite.com " from PHP (I will be using a cronjob, but for now I just click mysite.com/cronJobs/parseMail in my browser).

This is my first time trying to parse emails .. so I'm just not sure how to fix the problem at all.

Here is the code I am using, found it on the site and it felt like I could work. (Yes, I replaced all placeholders)

        $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);
    }

      

The problem is I get this when I hit it

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

      

So I don't know what it means. I have googled and all the results show it is a "mystery bug". It looks like he's about to pull the body out a third time (I really don't know what this weird line is ... maybe there is an email id?) .. but then he chokes when he didn't choke the previous two times.

.. can anyone give any ideas on how I should move forward?

EDIT

so I reduced the parseMail function to a minimum ..

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

      

and I still get the error when I hit it. Ideas?

solved

It looks like it has something to do with Codeigniter interpreting errors / warnings thrown by IMAP functions as exceptions.

My solution was to put this after my imap_close () function

imap_errors();

      

This saved me the error notification.

This is where I found my solution on Kohana message board

+3


source to share





All Articles