Results 1 to 4 of 4

Thread: checkgmail.py -- GMail Command Line

  1. #1
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915

    checkgmail.py -- GMail Command Line

    Hey Hey,

    Since this forum hasn't been used in a while, it's time to use it again... here's my lastest bit of playing around... a small simple implementation of libgmail from http://libgmail.sourceforge.net/.

    Of course you'll need to go there and download libgmail and have it available to the script...

    Here's the code

    Code:
    #!/usr/bin/python
    '''
    Name: checkgmail.py
    Created by: HTRegz (ht@computerdefense.org)
    Date Created: April 27th, 2006
    Functional Spec: Provide command line access to gmail.  Details and examples @ www.computerdefense.org)
    '''
    
    import libgmail, sys, getopt, getpass
    
    def parseArgs():
        try:
            opts, args = getopt.getopt(sys.argv[1:], "aruhL:F:RSCQ:N:U:P:")
        except getopt.GetoptError:
            showHelp()
        global password
        global type
        global label
        global showMsg
        global address
        global showSubject
        global showCount
        global query
        global displayCount
        global username
        password = None
        type = "UNREAD"
        label = "inbox"
        showMsg = False
        address = None
        showSubject = False
        showCount = False
        query = None
        username = None
        displayCount = None
        for o, a in opts:
            if ( o == "-a" ):
                type = "ALL"
            elif ( o == "-u" ):
                type = "UNREAD"
            elif ( o == "-r" ):
                type = "READ"
            if ( o == "-L" ):
                label = a
            if ( o == "-F"):
                address = a
            if ( o == "-R" ):
                showMsg = True
            if ( o == "-S" ):
                showSubject = True
            if ( o == "-C" ):
                showCount = True
            if ( o == "-Q" ):
                query = a
            if ( o == "-U" ):
                username = a
            if ( o == "-P" ):
                password = a
            if ( o == "-N" ):
                displayCount = a
            if (o == "-h" ):
                showHelp()
        
    def showHelp():
        print "Usage: "+ sys.argv[0] + " <option> <parameters>"
        print "\n"
        print "Options:"
        print "-a               All messages"
        print "-r               Only read messages"
        print "-u               Only unread messages"
        print "-h               Display this help message"
        print "Parameters:"
        print "-L <label>       Search only in a specific label (default: inbox)"
        print "                 Use ALL to search all folders"
        print "-F <address>     Only messages from a specific address"
        print "-R               Read the messages (display message content)"
        print "-S               Return the subject lines"
        print "-C               Display a message count"
        print "-Q <query>       Run the specified query (search)"
        print "-N               Number of messages to return (default: ALL)"
        print "-U <username>    GMail username"
        print "-P <password>    GMail Password (default: prompt)"
        sys.exit()
    
    
    if __name__ == "__main__":
        parseArgs()
        if ( username == None ):
            username = raw_input("Username: ")
        if ( password == None ):
            password = getpass.getpass("Password: ")
        ga = libgmail.GmailAccount(username, password)
        try: 
            
            ga.login()
        except libgmail.GmailLoginFailure:
            print "Login Failure: Unknown username/password."
            sys.exit()
        if ( type == "ALL" ):
            search = ""
        elif ( type == "READ" ):
            search = "is: read "
        else:
            search = "is: unread "
        if ( address != None ):
            search += "from: " + address + " "
        if ( label != None ):
            search += "in: "+ label + " "
        if ( query != None ):
            search  += query + " "
        tmp = ga.getMessagesByQuery(search, "allPages")
        counter = 0
        for thread in tmp:
            for msg in thread:
                counter += 1
                if ( showSubject == True ):
                    print msg.subject
                if ( showMsg == True ):
                    print msg.source
                if ( counter == displayCount ):
                    break
            if ( counter == displayCount ):
                break
        if ( showCount == True ):
            print counter
    You can find some examples and more information on my blog post related to it, I'll also have follow up examples in the next couple days.. -- http://www.computerdefense.org/?p=54

    Peace,
    HT

  2. #2
    Member
    Join Date
    Aug 2002
    Location
    San Antonio, Texas
    Posts
    49
    Wow really cool. Did you write that script yourself?
    \"They have the internet on computers now?\"

  3. #3
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Originally posted here by ViRalNeXxUs
    Wow really cool. Did you write that script yourself?
    I wrote the script yes... not the libgmail library... but the script is 100% original...

  4. #4
    Member
    Join Date
    Aug 2002
    Location
    San Antonio, Texas
    Posts
    49
    I showed my friend and she liked it too. It's so simple yet effective. gj
    \"They have the internet on computers now?\"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •