views:

46

answers:

0

I'm sorting through emails looking for specific instances of strings and it is taking too long. I would like to get the time down to half a second per email, and it's currently taking about 2-2.5 seconds per email.

I'm worried I'm doing something really dumb that's slowing this down - probably with mysql or zend_email. What this code does is it checks a user's inbox for a specific phrase "chocolate", then returns values back to the jquery ajax function. It loops through ten times (in this version, it checks 10 emails). If you see anything that will help decrease loading time, it would be appreciated. Initially, I thought not including the libraries would be helpful, but the libraries without the email opening functions was lightning fast.

I'm sure I'm doing something dumb and amateurish (maybe a few things). Please point them out if possible.

Here's the code...

<?php
        $storage = new Zend_Mail_Storage_Imap($imap);


    $x=0;
    while($x<10)
    {
            $flags = $storage->getMessage($i)->getFlags();      

            if(!empty($flags['\Seen']))
            {
                $read=1;
            }

            else 
            {
                $read=0;
            }


        if (strpos($storage->getMessage($i),'chocolate') !== FALSE ) 
        {
            $fromaddress = str_replace("'","",$storage->getMessage($i)->from);
            $fromaddress = str_replace('"','',$fromaddress);

            $sql = "SELECT `senderemail`,`subscribed` FROM email_spam WHERE `useremail` = '$_SESSION[email_address]' AND `senderemail` = '$fromaddress'";   $result = mysql_query($sql) or die (mysql_error());

            $num = mysql_num_rows($result);

            if($num == 0)
            {
                $emailmessage = mysql_escape_string($storage->getMessage($i)->getContent());



                $sql_insert = "INSERT into `email_spam` (`message`,`useremail`,`senderemail`,`datetime`,`subscribed`) VALUES ('$emailmessage','$_SESSION[email_address]','$fromaddress',now(),1)";//        echo $sql_insert;

                mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());

                $sql = "SELECT `emailid`,`datetime` FROM email_spam WHERE `useremail` = '$_SESSION[email_address]' ORDER BY `datetime` desc";       $getid = mysql_query($sql) or die (mysql_error());

                $num = mysql_num_rows($getid);

                echo '<tr><td>'. $fromaddress . '</td>';

                echo '<td class="unsubscribe_td" align="center"><input type="submit" value="Unsubscribe Me" class="unsubscribe_button" id="'. mysql_result($getid,0,'emailid') .'"/></td></tr>';

            }
        }



        if ($read==0)
        {

            $storage->setFlags($i, array(Zend_Mail_Storage::FLAG_RECENT)); //marks as new
        }   


        $i--;
        $x++;
    }

    ?>