B
B
Bergis2021-06-06 00:49:42
Python
Bergis, 2021-06-06 00:49:42

How can I fix the error with imap?

I have code:

import imaplib, email, os

def code_from_email(user, password):
  imap_url = "imap.mail.yahoo.com"
  connection = imaplib.IMAP4_SSL(imap_url)
  connection.login(user, password)
  connection.select("INBOX")
  start_message_uid = 1100
  result, data = connection.uid('search', None, "FROM", '[email protected]')
  code = ''
  while code == '':
    if result == 'OK':
      for num in data[-1].split():
        result, data = connection.uid('fetch', num, '(RFC822)')
        if result == 'OK':
          email_message = email.message_from_bytes(data[0][1])
          code = str(email_message.get_payload()[1])
  connection.close()
  connection.logout()
  return code   

f = open('emails.txt', 'r')
for line in f.readlines():
  user = line.split(':')[0]
  password = line.split(':')[1]
  print(user, password)
  code  = code_from_email(user, password)


The essence of the code - we read from the file all the logins and passwords of the mails, then we read the contents of the letter of the desired sender.
The problem is that when we pass 2 variables to the function, we get an error in response
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 610, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 1055, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: LOGIN command error: BAD [b'[CLIENTBUG] LOGIN Command arguments invalid']


If we replace 2 variables with 2 strings
code  = code_from_email('[email protected]', '12345678')

Then there will be no error and we successfully read the letter.
Please tell me what this error can be connected with and how to solve it

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question