Check Your IMAP Quota Using Python Imaplib

pythonThe python-imaplib module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in RFC 2060.

Here’s a code sample that returns your gmail quota details.

import getpass, imaplib, re
 
p = re.compile('\d+')
 
IMAP_SERVER='imap.gmail.com'
IMAP_PORT=993
IMAP_USERNAME='username@gmail.com'
 
M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
M.login(IMAP_USERNAME, getpass.getpass())
quotaStr = M.getquotaroot("INBOX")[1][1][0]
r = p.findall(quotaStr)
if r == []:
  print "Unlimited Quota Account"
  r.append(0)
  r.append(0)
 
print 'Allotted = %f MB'%(float(r[1])/1024)
print 'Used = %f MB'%(float(r[0])/1024)
M.logout()

The script will ask your gmail password and the output will be something like this:

Allotted = 7476.760000 MB
Used = 961.390000 MB