Check unread count of Gmail messages with Python

后端 未结 7 1356
面向向阳花
面向向阳花 2020-12-22 17:41

How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.

相关标签:
7条回答
  • 2020-12-22 18:04

    Once you are logged in (do this manually or with gmail.py) you should use the feed.

    It is located here: http://mail.google.com/mail/feed/atom

    It is the way Google does it. Here is a link to their js chrome extension: http://dev.chromium.org/developers/design-documents/extensions/samples/gmail.zip

    You will then be able to parse xml that looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <feed version="0.3" xmlns="http://purl.org/atom/ns#">
    <title>Gmail - Inbox for yourmail@gmail.com</title>
    <tagline>New messages in your Gmail Inbox</tagline>
    <fullcount>142</fullcount>
    
    0 讨论(0)
  • 2020-12-22 18:11

    Use Gmail.py

    file = open("filename","r")
    usr = file.readline()
    pwd = file.readline()
    gmail = GmailClient()
    gmail.login(usr, pwd)
    unreadMail = gmail.get_inbox_conversations(is_unread=True)
    print unreadMail
    

    Gets login information from a text file assuming the login name and password are on separate lines.

    0 讨论(0)
  • 2020-12-22 18:18

    Well, I'm going to go ahead and spell out an imaplib solution as Cletus suggested. I don't see why people feel the need to use gmail.py or Atom for this. This kind of thing is what IMAP was designed for. Gmail.py is particularly egregious as it actually parses Gmail's HTML. That may be necessary for some things, but not to get a message count!

    import imaplib, re
    conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    conn.login(username, password)
    unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
    

    Pre-compiling the regex may improve performance slightly.

    0 讨论(0)
  • 2020-12-22 18:19

    For a complete implementation of reading the value from the atom feed:

    import urllib2
    import base64
    from xml.dom.minidom import parse
    
    def gmail_unread_count(user, password):
        """
            Takes a Gmail user name and password and returns the unread
            messages count as an integer.
        """
        # Build the authentication string
        b64auth = base64.encodestring("%s:%s" % (user, password))
        auth = "Basic " + b64auth
    
        # Build the request
        req = urllib2.Request("https://mail.google.com/mail/feed/atom/")
        req.add_header("Authorization", auth)
        handle = urllib2.urlopen(req)
    
        # Build an XML dom tree of the feed
        dom = parse(handle)
        handle.close()
    
        # Get the "fullcount" xml object
        count_obj = dom.getElementsByTagName("fullcount")[0]
        # get its text and convert it to an integer
        return int(count_obj.firstChild.wholeText)
    
    0 讨论(0)
  • 2020-12-22 18:19

    Well it isn't a code snippet but I imagine using imaplib and the Gmail IMAP instructions get you most of the way there.

    0 讨论(0)
  • 2020-12-22 18:24
    import imaplib
    obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
    obj.login('username','password')
    obj.select()
    obj.search(None,'UnSeen')
    
    0 讨论(0)
提交回复
热议问题