Search code examples
phpzend-frameworkzend-mail

Zend Mail - how to read without emails being marked as opened


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.


Solution

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