PHP Displaying Unread Mail Count

PHP Displaying unread mail count

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/

Using IMAP () in PHP to get recent unread emails count

This function seems to work:

function CountUnreadMail($host, $login, $passwd) {
$mbox = imap_open($host, $login, $passwd);
$count = 0;
if (!$mbox) {
echo "Error";
} else {
$headers = imap_headers($mbox);
foreach ($headers as $mail) {
$flags = substr($mail, 0, 4);
$isunr = (strpos($flags, "U") !== false);
if ($isunr)
$count++;
}
}

imap_close($mbox);
return $count;
}

Usage:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user@gmail.com';
$password = 'user_password';

$count = CountUnreadMail($hostname, $username, $password);

I can’t claim full credit for this function. It’s a slightly edited version of sdolgy’s answer to PHP Displaying unread mail count. His version assumed POP mail. This version requires the full $hostname. I tested it with my own gmail account and it correctly reported the number of unread messages I had in my inbox.

PHP Displaying unread mail count has some pretty good reading material. Check it out.

Hope this helps.

UPDATE

From: Does Gmail support all IMAP features?

Gmail IMAP1 is a fairly complete implementation of IMAP, but the
following features are currently unsupported:

\Recent flags on messages.

Verfied at: Gmail's Buggy IMAP Implementation

Gmail doesn't handle standard IMAP flags, such as "\Deleted",
"\Answered", and "\Recent".

See also: Jyoti Ranjan's answer (below) for a possible solution.

How to get Gmail unread count

You can also use the Gmail Inbox Feed to get the unread count.
Just send an authenticated GET request to https://mail.google.com/mail/feed/atom and check the value of the fullcount element.

PHP - imap count unseen emails gives always '1' as result

imap_search() returns an array, not a number, according to the documentation.

So instead you need:

$result = imap_search($imap, 'UNSEEN');
echo count($result);

OK, sorry, miss interpreted the documentation myself. So here an explanation of your issue: the function indeed does return an array, but an array holding one result (count) per search attribute you handed over. Since you only specify a single attribute ('UNSEEN') you always get a single element in the array. That elements value is the number of messages matching that search criteria.

Therefore the correct usage should be:

$result = imap_search($imap, 'UNSEEN');
if (is_array($result) && isset($result[0])) {
echo count($result[0]);
} else {
echo "Failed to query number of messages\n";
}

Check for unread mail in php

It is possible, if you implement an IMAP (or POP3) client in your PHP script. When you open your page, PHP would connect to the mail server and check for new messages. To achieve this, PHP would need your username/password and server address/port. Hence, this information will have to be stored on the server.

The example given at http://lv.php.net/imap_mailboxmsginfo will give you some more hints.

Messages counter is showing total number of messages instead of count for logged user in Laravel

You need to pass the current user id to your query, remove the raw query

$currentUserId = Auth::user()->id;
$unviewedMessagesCount = Message::where('read_state', '0')
->where('from_admin', '0')
->where('user_id',$currentUserId)
->count();


Related Topics



Leave a reply



Submit