I use zend email expanded with zend_mail_storage_imap, and I created an application that searches for keywords in a user's email.
The problem is that it opens every email and saves it as read. Is there a way to check the body of letters and not mark every mail scanned as read?
Here is the current working code. This is part of an ajax request that automatically scans someone's mailbox. In this current form, it will mark every mail, starting with the most recent user mail read (in gmail). Is it possible to check the body text, but not mark it as read. Alternatively, will I need to check whether each mail is read or not read before looking for it, and then restore it to this 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 is the latest 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 whole code is looped, so here I run the entire search / sort algorithm for each individual letter.
if ($read==0)
{
$storage->setFlags($i, array(Zend_Mail_Storage::FLAG_RECENT));
}
, ( ), . , ( ) . .