Search code examples
pythonimapgmail-imapimaplib

TypeError: can't concat bytes to IMAP4_SSL


I would like to contact the google mail servers, but all I get the following error code:

TypeError: can't concat bytes to IMAP4_SSL

I have udpated my account settings in google as well to enable IMAP

My code so far (very basic, I know :) ):

import imaplib

mail = "[email protected]"
pwd = "pwd"
smtp_server = "imap.gmail.com"
smtp_port = 993

mail = imaplib.IMAP4_SSL(smtp_server)
mail.login(mail,pwd)

My questions:

  1. What does this error code mean?
  2. Why do I get it?
  3. How can I solve it?

Thank you very much for helping. Currently Im just playing around in Python, but I have a hard time to understand this. Also Im already stuck on this for some time.

Regards, Sjaak

Update:

Hi Max, See below the error it generates:

    mail.login(mail,pwd)
  File "/usr/lib/python3.5/imaplib.py", line 580, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib/python3.5/imaplib.py", line 1180, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python3.5/imaplib.py", line 945, in _command
    data = data + b' ' + arg
TypeError: can't concat bytes to IMAP4_SSL

The version of Python: 3.5.2

Thx.

Regards, Sjaak –

Update:

It worked! thx for helping out


Solution

  • You first define mail as a string: mail = "[email protected]"
    You then redefine it as your new IMAP object: mail = imaplib.IMAP4_SSL(smtp_server)

    It can't be both of these things at once, so now mail is the IMAP4_SSL connection object.

    You then do mail.login(mail,pwd), passing the connection object, rather than the email address as you wanted.

    You can fix this easily by changing one definition or the other to another name:

    import imaplib
    
    username = "[email protected]"
    pwd = "pwd"
    imap_server = "imap.gmail.com"
    imap_port = 993
    
    conn = imaplib.IMAP4_SSL(imap_server, imap_port)
    conn.login(username, pwd)
    

    I've changed them both for clarity. Also an IMAP server is not the same as an SMTP server, so you may want to be more careful with your variable names, so I've changed that as well. You also did not use your port variable.