Python 3.0 smtplib

pythonpython-3.x

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 = 'mail.somedomain.com'
   AUTHREQUIRED = 1                     # if you need to use SMTP AUTH set to 1
   smtpuser = 'admin@somedomain.com'    # for SMTP AUTH, set SMTP username here
   smtppass = 'somepassword'            # for SMTP AUTH, set SMTP password here
   msg = "Some message to send"

   RECIPIENTS = ['admin@somedomain.com']
   SENDER = 'someone@someotherdomain.net'

   session = smtplib.SMTP(smtpserver)

   if AUTHREQUIRED:
      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 smtplib.py 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.