Last active
January 28, 2016 15:57
-
-
Save coccoinomane/baa12fc4619ae9e0c740 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* List all folders in an IMAP account, together with | |
* number of unread messages and number of total messages | |
* in each folder. | |
* | |
* In order to work, this script requires the presence of a | |
* file named 'accounts.php' in the same folder, containing | |
* the following variables: | |
* | |
* - $imap_server, eg. imap.gmail.com | |
* - $imap_options, eg. 993/imap/ssl | |
* - $accounts, eg. | |
* $accounts = [ | |
* ['user' => 'username1', 'pwd' => 'password1'], | |
* ['user' => 'username2', 'pwd' => 'password2'], | |
* ['user' => 'username3', 'pwd' => 'password3'], | |
* ]; | |
* | |
* In writing this script, I used the following resources; I wish to | |
* thank their authors: | |
* - http://incredibill.com/blog/php-imap-email-part-1-how-to-open-an- | |
* imap-mailbox-and-display-folder-in-php/ by IncrediBILL | |
* - http://www.sitepoint.com/exploring-phps-imap-library-1/ by Rakhitha | |
* Nimesh and Timothy Boronczyk | |
* - http://www.hackingwithphp.com/15/6/1/opening-a-mailbox by Paul | |
* Hudson. | |
* | |
* Created by Guido Walter Pettinari on 28/01/2016 | |
* Last version here: https://gist.github.com/baa12fc4619ae9e0c740 | |
*/ | |
/* Load account information */ | |
include 'accounts.php'; | |
/* Loop over the number of users */ | |
$n_users = count ($accounts); | |
foreach ($accounts as $account) { | |
/* Connect to the IMAP server */ | |
$imap_string = "{" . $imap_server . ":" . $imap_options . "}"; | |
$user = $account['user']; | |
$pwd = $account['pwd']; | |
$imap_stream = imap_open($imap_string, $user, $pwd); | |
if ($imap_stream) { | |
echo "$user connected $imap_server\n"; | |
} | |
else { | |
error_log ("Failed to connect $user to $imap_server"); | |
continue; | |
} | |
/* List folders in mailbox */ | |
$folders = imap_list($imap_stream, $imap_string, "*"); | |
$n_folders = count ($folders); | |
printf ("Listing %d folders for user %s on %s\n", $n_folders, $user, $imap_server); | |
foreach ($folders as $folder) { | |
$status = imap_status ($imap_stream, $folder, SA_ALL); | |
$folder_name = str_replace($imap_string, "", imap_utf7_decode($folder)); | |
printf ("- %-30s (%8d messages, %8d unread)\n", $folder_name, $status->messages, $status->unseen); | |
} | |
imap_close ($imap_stream); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment