Zend Mail - how to read without messages marked as open

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)); //marks as new
}   

, ( ), . , ( ) . .

+3
5

. . imap setFlags. Api

+3

"seen":

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

"" , ! gmail "".

+2

IMAP Zend Framework setFlags Zend_Mail_Storage_Imap

, ZF, API (. ), / .

0
source

You can also just use an empty array to reset any flags

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

Changing the flag is apparently done using the getContent () method after retrieving the message in zend-mail 2. The following is an example of reading the contents and saving the 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);
0
source

Source: https://habr.com/ru/post/1766973/


All Articles