Search code examples
pythonimaplib

python imaplib UID command error: BAD [b'Could not parse command']


I am trying to copy a mail using the copy command.

imapper.copy(email.uid, 'TEST')

email.uid = the uid in this case 1069

TEST = the map the mail should be in

This is my copy function:

def copy(self, uid, to):
    #typ, content = self._mailer.copy(bytes(uid), to)
    print (uid)
    typ, content = self._mailer.uid('copy', bytes(1069), 'TEST')
    if typ == 'OK':
        mail = _parse_email(content, include_raw=include_raw)
        return mail
    else:
        raise Exception("Could not copy email.")

Since I'm having this error i decided to NOT use variables and hardcode it until it works and than replace it with the variables.

typ, content = self._mailer.uid('copy', bytes(1069), 'TEST')

This returns the error:

UID command error:BAD [b'Could not parse command']

I have added bytes() otherwise i get this error:

TypeError: can't concat int to bytes


Solution

  • It takes a string. use str(1069) or '1069', not an integer. Although, they are numbers, the protocol treats them as strings, not numbers.

    bytes(1069) in python creates a byte array of 1069 zeros, and so you're sending a bunch of nulls.