Asked  7 Months ago    Answers:  5   Viewed   36 times

I am using php imap class. In my box I have a lot of mail, but with this script I would retrieve only the unreaded mail. How can I do it?

if ($mbox=imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass )) 
{
  echo "Connectedn"; 
} else { exit ("Can't connect: " . imap_last_error() ."n");  echo "FAIL!n";  }; 

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
  echo "Ci sono ".$msgCount." mail";
} else {
  echo "Failed to get mail";
}

If I do

$overview=imap_fetch_overview($mbox,"1:$msgCount",0);

the script load to an infinity time.

The imap_search UNSEEN solution is not good because pop3 don't use this flag. So how can I do?????? Thanks a lot.

 Answers

41

There is two way you can follow:

1. Looping through the messages

$count = imap_num_msg($connection);
for($msgno = 1; $msgno <= $count; $msgno++) {

    $headers = imap_headerinfo($connection, $msgno);
    if($headers->Unseen == 'U') {
       ... do something ... 
    }

}

2. Using imap_search

There's a flag called UNSEEN which you can use to search for the unread emails. You would call the imap_search function with the UNSEEN flag like so:

$result = imap_search($connection, 'UNSEEN');

If you need to combine this with more search flags, for example searching for messages from me@example.com, you could do this:

$result = imap_search($connection, 'UNSEEN FROM "me@example.com"');

For a complete list of the available flags, refer to the criteria section of the imap_search manual page on the PHP website (www.php.net/imap_search)

Source: http://www.electrictoolbox.com/php-imap-unread-messages/

Wednesday, March 31, 2021
 
Fishingfon
answered 7 Months ago
95

http://ca.php.net/imap

$mb = imap_open("{host:port/imap}","username", "password" );

$messageCount = imap_num_msg($mb);
for( $MID = 1; $MID <= $messageCount; $MID++ )
{
   $EmailHeaders = imap_headerinfo( $mb, $MID );
   $Body = imap_fetchbody( $mb, $MID, 1 );
   doSomething( $EmailHeaders, $Body );
}
Saturday, May 29, 2021
 
kwichz
answered 5 Months ago
53

I'd suggest 4'th:

Once new message has been sent to a user, you update counter in memcache. You create simple ajax application on client side sending a request every X seconds. At server side, you just check is there unread messages. At page refresh, you don't need to query the database since you get count from memcache extremely fast.

That's what I'd done if I had bottleneck in DB (in 90% cases, DB is the weekest part of any database-driven application).

That's what we usually do at highloaded web sites: we trying to avoid any COUNTER queries. If not, we denormalize the database to store counters right in the appropriate table as yet another column e.g. if you can not use memcache, you would store the unread messages counter as a column for Users table.

Saturday, May 29, 2021
 
Webroots
answered 5 Months ago
92

Unfortunately, e-mail clients can essentially do whatever they want with your message, and there is no reliable standard for determining how a received message originated at the client. In addition, IMAP doesn't really have anything to do with it. E-mails can be sent a number of different ways, including webmail.

The best you can do is look for an ID number in the subject line (assuming folks don't change it, which they rarely do). You can also do what Google does... fuzzy match the reply text to e-mail you sent to that address. If it matches, consider it part of the reply. This takes great effort though.

Wednesday, August 11, 2021
 
mistero
answered 3 Months ago
68

This bug was related to configuration mbstring.func_overload in php.ini. You can use workaround to avoid this bug - just add at begin of your script:

if (function_exists('mb_internal_encoding')) {
    $oldEncoding=mb_internal_encoding();
    mb_internal_encoding('latin1');
}

and at the end:

if (function_exists('mb_internal_encoding'))
    mb_internal_encoding($oldEncoding);

It helped me, so, I believe it'll help you as well.

Saturday, August 21, 2021
 
aWebDeveloper
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :