php-imap
php-imap copied to clipboard
Message setFlag('seen') looks to not working
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
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);