Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

   RECIPIENTS = ['[email protected]']
   SENDER = '[email protected]'

   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.

like image 253
Adam Avatar asked Nov 01 '25 22:11

Adam


1 Answers

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.

like image 102
Jay Avatar answered Nov 03 '25 12:11

Jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!