开发者

What's the most efficient way to sort through emails with zend_mail?

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`,`subsc开发者_开发问答ribed`) 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++;
    }

    ?>


There's no point in profiling this - it's just the wrong approach. You're doing a separate IMAP request and data transfer for every message, which will never be fast. The most efficient way is not to do it here at all - Get the IMAP server to do the search for you. It's probably not quite as flexible as what you're doing, but it will have no trouble searching tens of thousands of messages per second, and it can certainly do a simple string match in message bodies. PHP has support for searching inboxes in the IMAP extension. I don't know if that function is exposed via any of the Zend components, but it doesn't really matter if it isn't.

This is exactly the mechanism that iPhone inbox search uses; It does local search on messages that it already has, but then offers the option to continue the search on the server, which hands-off to something that's already got the data and has more horsepower to bring to bear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜