Zend Mail - 如何在没有电子邮件标记为已打开的情况下阅读

I'm using zend mail extended with zend_mail_storage_imap and I built an application that looks for keywords in user's emails.

The problem is that it opens up each email and keeps it marked as read. Is there a way to check the body of emails and not mark each mail checked as read?

Here's current working code. It's part of an ajax query that automatically looks through someone's inbox. In this current form, it will mark each mail starting with a user's most current mail as read (in gmail). Would it be possible to check the body text, but not mark the email as read. Alternatively, will I need to check if each mail is read or unread before looking it up, and then restore it to that state as a workaround?

if (strpos(htmlentities($storage->getMessage($i)),$searchterm)) 
{
    $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)";

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

    }

}

EDIT - here's the final code for those interested

$storage = new Zend_Mail_Storage_Imap($imap);


$flags = $storage->getMessage($i)->getFlags();      
$newflag = $flags[Zend_Mail_Storage::FLAG_RECENT];  
$oldflag = $flags['\Seen'];

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

The entire code is looped, so here, I perform my entire searching/sorting algorithm for each individual email.

if ($read==0)
{
    $storage->setFlags($i, array(Zend_Mail_Storage::FLAG_RECENT)); //marks as new
}   

Here, I go and mark the emails that were not read (before the implementations) as unread. I think this is the most efficient way (that I could find) of performing this operation. I welcome any other codes or comments.

After reading a message, you could unset the seen flag. See also the imap implementation of the setFlags method. Api documentation

When reading mails with the IMAP Storage in Zend Framework you have access to a method called setFlags in Zend_Mail_Storage_Imap

I don't think it's documented in the ZF manual but you might want to look into the API docs (see link above) to set the status/flag on a message.

To unset the "seen" flag:

$flags = $msg->getFlags();
unset($flags[Zend_Mail_Storage::FLAG_SEEN]);
$storage->setFlags($i, $flags);

Setting the "recent" flag does not necessarily do what you want it to! On gmail, it will mark emails as "important".

You could also just use empty array to reset any flags

$mailstorage->setFlags($messageID, array());

Flag change seem to be done via getContent() method after fetching the message in zend-mail 2. Below is an example to read content and keep initial flags :

$imap = [
    'host'     => $connection['mailhost'],
    'user'     => $connection['username'],
    'password' => $password,
];
$storage = new \Zend\Mail\Storage\Imap($imap);
$lastMsgIndex = $storage->countMessages();
$msg = $storage->getMessage($lastMsgIndex);
$msgFlags = $msg->getFlags();
// Line below will mark email as seen if getContent is called
$content = $msg->isMultipart() ? 'Multipart Email' : $msg->getContent();
$storage->setFlags($lastMsgIndex, $msgFlags);