'imaplib.IMAP4.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED in python

** This is my code to access and searching the trash box of email through imap4 library**


def trash(request):
    global i, addr, passwrd, conn
    if request.method == 'POST':
        imap_url = 'imap.gmail.com'
        conn = imaplib.IMAP4_SSL(imap_url)
        conn.login(addr, passwrd)
        conn.select('"[Gmail]/Trash"')
        result1, data1 = conn.search(None, "ALL")
        mail_list = data1[0].split()
        text = "You have reached your trash folder. You have " + str(len(mail_list)) + " mails in your trash folder. To search a specific email say search. To go back to the options page say back. To logout say logout."
        TextToSpeech(text,200)
        flag = True
        while (flag):
            act = speechtotext(5,'yes or no')
            act = act.lower()
            print(act)
            if act == 'search':
                flag = False
                emailid = ""
                while True:
                    TextToSpeech("Enter email ID of sender.",230)
                    emailid = speechtotext(15,'email of sender')
                    TextToSpeech("You meant " + emailid + " say yes to confirm or no to enter again",230)
                    yn = speechtotext(5,'yes or no')
                    yn = yn.lower()
                    if yn == 'yes':
                        break
                emailid = emailid.strip()
                emailid = emailid.replace(' ', '')
                emailid = emailid.lower()
                emailid = convert_special_char(emailid)
                search_specific_mail('"[Gmail]/Trash"', 'FROM', emailid, 'trash')

            elif act == 'back':
                TextToSpeech("You will now be redirected to the options page.",230)
                conn.logout()
                return JsonResponse({'result': 'success'})

            elif act == 'logout':
                addr = ""
                passwrd = ""
                TextToSpeech("You have been logged out of your account and now will be redirected back to the login page.",230)
                return JsonResponse({'result': 'logout'})

            else:
                TextToSpeech("Invalid action. Please try again.",230)

            TextToSpeech("If you wish to do anything else in the trash folder or logout of your mail say yes or else say no.",230)
            ans = speechtotext(3,'yes or no')
            ans = ans.lower()
            print(ans)
            if ans == 'yes':
                flag = True
                TextToSpeech("Enter your desired action. Say search, back or logout. ",230)

        TextToSpeech("You will now be redirected to the options page.",230)
        conn.logout()
        return JsonResponse({'result': 'success'})
    elif request.method == 'GET':
        return render(request, 'trash.html')

Error:line no 666 is ** result1, data1 = conn.search(None, "ALL") plz help** * Traceback (most recent call last): File "C:\Users\Shaibaz\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\Shaibaz\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Shaibaz\Desktop\khizar dont delete very important\ project\demo\demo\views.py", line 666, in trash result1, data1 = conn.search(None, "ALL") File "C:\Users\Shaibaz\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 723, in search typ, dat = self._simple_command(name, *criteria) File "C:\Users\Shaibaz\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1196, in _simple_command return self._command_complete(name, self._command(name, *args)) File "C:\Users\Shaibaz\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 944, in _command ', '.join(Commands[name]))) imaplib.IMAP4.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED *



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source