1

I'm trying to count all emails from and email but my script only count mails from inbox,

anyone know how to count all emails from the mail account including sent,spam,deleted, etc

$mailcnf = "mail.office365.com:993/imap/ssl/novalidate-cert";
$conn_str = "{".$mailcnf."}INBOX";


$username = 'test3@sjnewman.co.uk';
$password = 'Woju6532';
$imap = imap_open($conn_str,$username,$password) or die('Cannot connect to Server: ' . imap_last_error());

echo $message_count = imap_num_msg($imap);
  • I think the `INBOX` is the cause for being in the inbox. Take a look at http://www.electrictoolbox.com/open-mailbox-other-than-inbox-php-imap/. – chris85 Mar 20 '16 at 16:00

2 Answers2

0

first use imap_list to list all available folders. then $conn_str = "{".$mailcnf."}$mailbox"instead of mailbox imap_num_msg should return the number of emails in the current mailbox

Kassav'
  • 1,130
  • 9
  • 29
0

You can loop through each folder and use imap_status() to count the number of emails in each folder. Here's an example:

<?php
$username = 'mail@example.com';
$password = 'password123';

// Define the connection string:
$server = '{server.example.net:993/ssl}';

// Connect to the server:
$connection = imap_open($server, $username, $password);

// List the mailboxes:
$mailboxes = imap_list($connection, $server, '*');

// Loop through the mailboxes:
foreach($mailboxes as $mailbox) {
 $status = imap_status($connection, "$mailbox", SA_ALL);
    if ($status) {
      echo "Mailbox: $mailbox\t\tMessages: " . $status->messages . "\n";
    } else {
        echo "imap_status failed: " . imap_last_error() . "\n";
    }
}

// Close the connection:
imap_close($connection);
?>
rkhff
  • 1,696
  • 2
  • 16
  • 16