Retrieve Gmail/GApps Message Count
Long ago I switched from using any sort of mail client to using the webmail interface of Gmail/Google Apps. This switch was really fueled by my increased usage of the interface after being hired by Google. I won’t go on and on about using the web interface, but sometimes it is nice to know how many actual emails are in your account. Google mail only shows you the number of threaded emails, not each individual response. If I look in “All Mail” for my manis@digital39.com account I see “1 – 100 of 21758″, which represents the number of email threads, the actual number of responses is 56578. FYI, this requires that you enable IMAP support for the account.
$ imap_count.py manis@digital39.com
Password for manis@digital39.com: *****
56578 messages in account manis@digital39.com
Expand the code below and copy it to a file on your system. It takes one argument, and that argument is a comma separated list of email addresses.
#!/usr/bin/python2.4
#
# Copyright (c) 2009, Peter Manis
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# Neither the name of the Peter Manis nor the names of its contributors may
# be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import getpass
import imaplib
import sys
results = []
def RetrieveMsgCount(username, password):
"""Retrieves message count by accessing Gmail or Google Apps IMAP account.
Args:
username: str, Username of the account we are accessing.
password: str, Password of the account we are accessing.
Returns:
count: str, Number of messages in IMAP account
Raises:
imaplib.IMAP4.error: Failed to login to the IMAP account.
"""
try:
imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap.login(username, password)
status, [count] = imap.select ("[Gmail]/All Mail")
imap.close()
imap.logout()
except imaplib.IMAP4.error:
return 'Unable to check'
else:
return count
def main():
if len(sys.argv) > 1:
accounts = sys.argv[1]
for account in accounts.split(','):
password = getpass.getpass("Password for %s: " % account)
results.append('%s messages in account %s' % (
RetrieveMsgCount(account, password), account))
for account in results:
print account
else:
print "Please provide a comma separated list of accounts to check."
if __name__ == '__main__':
main()





