Webklex / php-imap

PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled. The protocol is completely integrated and therefore supports IMAP IDLE operation and the "new" oAuth authentication process as well.
https://www.php-imap.com
MIT License
314 stars 147 forks source link

Message setFlag('seen') looks to not working #439

Open redfoman opened 1 year ago

redfoman commented 1 year ago

Hi,

I anticipated that the following code would mark the processed messages as 'Seen' (read) in the server mailbox, but it doesn't seem to be doing that. Am I overlooking something? (I am utilizing the legacy IMAP protocol and invoking the expunge method).

$cm = new ClientManager(Config::get('imap'));
try {
  $client = $cm->make([
        'host' => $mailBox['hostname'],
        'port' => $mailBox['port'],
        'encryption' => false,
        'validate_cert' => false,
        'username' => $mailBox['username'],
        'password' => $mailBox['password'],
        'protocol' => 'legacy-imap'
   ]);
   $client->connect();
} catch (\Exception $e) {
  exit('failed to connect');
}

//get unseen messages from inbox
$folder = $client->getFolder('INBOX');
$query = $folder->messages()->unseen();

//process messages
$messages = $query->get();
if ($messages->total() > 0) {
    foreach ($messages as $message) {
       $message->setFlag('seen'); //this has no effect

       $messageId = $message->getMessageId()->offsetGet(0);
       $messageBody = $message->getTextBody();//$message->getHTMLBody();

       ......//rest of the code
    }
}

$client->expunge();
$client->disconnect();

Thanks in advance

redfoman commented 1 year ago

note that the below (using php build in functions directly) works as expected:

$imapStream = imap_open("{".$mailBox['hostname'].":".$mailBox['port']."/imap/novalidate-cert}INBOX", $mailBox['username'], $mailBox['password']);
$unseenEmails = imap_search($imapStream, 'UNSEEN');
if ($unseenEmails) {
    foreach ($unseenEmails as $emailUid) {
        // Mark the email as read (Seen)
        imap_setflag_full($imapStream, $emailUid, "\\Seen");
    }
}
imap_close($imapStream);