简体   繁体   中英

Unable to get the unread mails from office365 mail box using PHP imap_headers() function

I was trying to read the gmail mail box using imap_headers and imap_header() function available in php, it was working fine without any problems. Here in gmail imap_header() function gives the count of only un-read mails and imap_headers($message_number) function would take the message number as input and would return the header information of the mail. Executing imap_fetchstructure() and imap_fetchbody() the message would be marked as read and that message would not come in the next run, since it was marked as read.

Now when I am using office365 account the function imap_header() is returning the count of all mails in the inbox and not the unread mails count, also as contrary in previous case imap_fetchstructure() and imap_fetchbody() are not marking the mail as read and hence the read mails also get tracked in the next job cycles.

Any inputs on this is really helpfull.....Thanks:)

The imap_header() will give you all the mail as its definition. You should use imap_search() to filter out the unread mail. You can use the below code:

$imapobj = imap_open(SERVER,USERNAME,PASSWORD);
$result = imap_search($imapobj, 'UNSEEN');
foreach($result as $res=>$value){   
      $maildetails = imap_headerinfo($value);  
      $status = imap_setflag_full($imapobj, $value, "\\Seen");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM