views:

428

answers:

3

I need to download emails from the gmail inbox only using poplib.Unfortunately I do not see any option to select Inbox alone, and poplib gives me emails from sent items too.

How do I select emails only from inbox?

I dont want to use any gmail specific libraries.

+3  A: 

POP3 has no concept of 'folders'. If gmail is showing you both 'sent' as well as 'received' mail, then you really don't have any option but to receive all that email.

Perhaps you would be better off using IMAP4 instead of POP3. Python has libraries that will work with gmail's IMAP4 server.

Jerub
Thanks... It helped. :-)
Mohit Ranka
A: 

This Java code would suggest that you can select a particular "folder" to download, even when using POP3. Again, this is using Java, not Python so YMMV.

How to download message from GMail using Java (blog post discusses pushing content into a Lucene search engine locally)

ewalk
+1  A: 

I assume you have enabled POP3/IMAP access to your GMail account.

This is sample code:

import imaplib
conn= imaplib.IMAP4_SSL('imap.googlemail.com')
conn.login('yourusername', 'yourpassword')
code, dummy= conn.select('INBOX')
if code != 'OK':
    raise RuntimeError, "Failed to select inbox"

code, data= self.conn.search(None, ALL)
if code == 'OK':
    msgid_list= data[0].split()
else:
    raise RuntimeError, "Failed to get message IDs"

for msgid in msgid_list:
    code, data= conn.fetch(msgid, '(RFC822)')
    # you can also use '(RFC822.HEADER)' only for headers
    if code == 'OK':
        pass # your code here
    else:
        raise RuntimeError, "could not retrieve msgid %r" % msgid

conn.close()
conn.logout()

or something like this.

ΤΖΩΤΖΙΟΥ