Fetch Gmail Archieved messages

Arshpreet Singh arsh840 at gmail.com
Tue Mar 15 06:48:03 EDT 2016


Hi, I am using imaplib to fetch Gmail's Inbox Archived message but results are not that much accurate. Here is code+logic:


def inbox_week():
    import imaplib
    EMAIL = 'myusername at gmail.com'
    PASSWORD = 'mypassword'
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login(  EMAIL, PASSWORD )
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.select("[Gmail]/All Mail")
    interval = (date.today()-timedelta(d)).strftime("%d-%b-%Y")
    _, data = mail.uid('search', None,'(SENTSINCE{date})'.format(date=interval))
        
    for num in data[0].split():
        _, data = mail.uid('fetch', num, '(BODY.PEEK[])')
    
        for response_part in data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                for header in ['to']:
               
# This is logic for inbox-archieved messages
                    if (EMAIL in str(msg[header]) in str(msg[header])):

                        main_tuple = email.utils.parsedate_tz(msg['Date'])   
                                
                        yield main_tuple

I am not sure how to get the list of only Inbox-messages as well as Sentbox messages from [All-mail]. Do I need to any other library ?



More information about the Python-list mailing list