Python 3.0 smtplib


I have a very simple piece of code that I used in previous versions of Python without issues (version 2.5 and prior). Now with 3.0, the following code give the error on the login line "argument 1 must be string or buffer, not str".

import smtplib

   smtpserver = ''
   AUTHREQUIRED = 1                     # if you need to use SMTP AUTH set to 1
   smtpuser = ''    # for SMTP AUTH, set SMTP username here
   smtppass = 'somepassword'            # for SMTP AUTH, set SMTP password here
   msg = "Some message to send"

   RECIPIENTS = ['']
   SENDER = ''

   session = smtplib.SMTP(smtpserver)

      session.login(smtpuser, smtppass)

   smtpresult = session.sendmail(SENDER, RECIPIENTS, msg)

Google shows there are some issues with that error not being clear, but I still can't figure out what I need to try to make it work. Suggestions included defining the username as b"username", but that doesn't seem to work either.

Best Solution

UPDATE: just noticed from a look at the bug tracker there's a suggested fix also:

Edit and replace the existing encode_plain() definition with this:

def encode_plain(user, password):
    s = "\0%s\0%s" % (user, password)
    return encode_base64(s.encode('ascii'), eol='')

Tested here on my installation and it works properly.