Python 3.0 smtplib

I have a very simple piece of code that I used in previous versions of Python without problems (version 2.5 and earlier). Now with 3.0, the following code gives an error in the input line "argument 1 must be a string or a 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 that some problems with this error are not clear, but I still cannot figure out what I need to get it to work. Suggestions included the definition of the username as "username", but this also does not work.

+3
source share
4 answers

UPDATE : Just noticed by looking at bug tracking, the following fix is ​​also offered there:

smtplib.py encode_plain() :

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

.

+4
Traceback (most recent call last):
  File "smtptest.py", line 18, in <module>
    session.login(smtpuser, smtppass)
  File "c:\Python30\lib\smtplib.py", line 580, in login
    AUTH_PLAIN + " " + encode_plain(user, password))
  File "c:\Python30\lib\smtplib.py", line 545, in encode_plain
    return encode_base64("\0%s\0%s" % (user, password))
  File "c:\Python30\lib\email\base64mime.py", line 96, in body_encode
    enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
TypeError: b2a_base64() argument 1 must be bytes or buffer, not str

. smtplib base64mime.py. : http://bugs.python.org/issue5259

, .

+3

Jay, , smtplib.py, "" .

- :


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

import smtplib
encode_plain.func_globals = vars(smtplib)
smtplib.encode_plain = encode_plain

, , python.

+2

Source: https://habr.com/ru/post/1703224/


All Articles