pythy
pythy

Reputation: 57

Python IMAP select multiple folders

I'm trying to get the informations from all the folders but it seems that the code gives me the following error:

command SEARCH illegal in state AUTH, only allowed in states SELECTED

I've googled it but no results for me.

This is the code:

        M = imaplib.IMAP4_SSL('',993)
        M.login(user,password)
        folders = M.list()

        for folder in folders[1]:

            for allfolders in re.findall('"\/"(.*)',folder):

                finalfolders = allfolders.replace(" ",'')

                M.select(finalfolders, readonly=True)

                print finalfolders

                typ, data = M.search(None, 'ALL')

                for num in data[0].split():
                    typ, data = M.fetch(num, '(RFC822)')
                    email_message = email.message_from_string(data[0][1])
                    su = email_message['From']

                    allz = re.findall("<(.*)>",su)

                    for x in allz:

                        print x

                    results.write(x+'\n')
                    results.flush()
                    #print su
                M.close()
                M.logout()

Basically I'm trying to fetch "From", from all the folders founded into my email account.

Upvotes: 1

Views: 3575

Answers (1)

Jan Kundr&#225;t
Jan Kundr&#225;t

Reputation: 3816

You can only have one folder selected at any given time using one IMAP connection. This means that your code should EXAMINE or SELECT a mailbox at first, then FETCH whatever you need to download, then do not call CLOSE because it removes messages marked for deletion, and upon entering the next loop iteration, call EXAMINE or SELECT once again, and...

Upvotes: 1

Related Questions