Friday, May 20, 2011

New mail count

This script is for showing new/unread email count in your INBOX.
You can use with conky or as a standalone script.


This script can handle several email services, such as gmail, squiremail, etc.
It is written in python, it's only disadvantage is that the passwords are stored in plaintext form in the script.

Here's the script: gmail.py

import imaplib
 # Username should be your username with '@gmail.com' added
 # If you're trying it with something other than gmail
 # you should make the username the full email address
 # example@domain.com
 username = 'user1234@gmail.com'
 password = 'secretPASSWD' # your pw
 mailbox = 'INBOX' # inbox is default
 # only tested with gmail and my university email
 # it should work with any imap server
 # change mail server and port to match your server's info
 mailserver = 'imap.gmail.com'
 port = 993
 # connect to gmail's server (uses SSL, port 993)
 server = imaplib.IMAP4_SSL(mailserver,port)
 # gmail uses ssl...if your imap mail server doesn't comment the above
 # line and uncomment this one.
 # server = imaplib.IMAP4(mailserver,port)
 # login with the variables provided up top
 server.login(username,password)
 # select your mailbox
 server.select(mailbox)
 # pull info for that mailbox
 data = str(server.status(mailbox, '(MESSAGES UNSEEN)'))
 # print it all out
 #print
 #print 'GMAIL' #change this if ur not using gmail
 #print username.replace('@gmail.com','') + '\'s Mailbox'
 tokens = data.split()
 print tokens[5].replace(')\'])','')
 # clean up output with str_replace()
 #print tokens[2].replace('(',''),tokens[3]
 #print tokens[4],tokens[5].replace(')\'])','')
 #print
 # close the mailbox
 server.close()
 # logout of the server
 server.logout()
All you need to change in this script is user name and password.
Run this script by typing python gmail.py
The script returns new/unread email count in the INBOX folder.

1 comment:

  1. Thanks for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing?
    Order personalised gifts online

    ReplyDelete