classification
Title: Trying To Concatenate Bytes and String in SMTPLIB
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: boom0192, corona10, gstarck
Priority: normal Keywords: patch

Created on 2021-02-04 13:23 by boom0192, last changed 2021-03-30 08:38 by corona10. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24476 merged gstarck, 2021-02-07 18:40
Messages (5)
msg386482 - (view) Author: Michael L. Boom (boom0192) Date: 2021-02-04 13:23
The space is string, and either mechanism and/or response are bytes.
smtplib.py:634
response = encode_base64(initial_response.encode('ascii'), eol='')
(code, resp) = self.docmd("AUTH", mechanism + " " + response)


import smtplib, ssl
port = 587  
smtp_server = "---------------"
smtpserver = smtplib.SMTP(smtp_server, port)
smtpserver.set_debuglevel(2)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login("", "")

-----------------------------------------------------
12:40:37.614737 send: 'ehlo [10.60.69.196]\r\n'
12:40:37.665528 reply: b'250-MWHPR20CA0007.outlook.office365.com Hello [65.54.12.68]\r\n'
12:40:37.665666 reply: b'250-SIZE 157286400\r\n'
12:40:37.665761 reply: b'250-PIPELINING\r\n'
12:40:37.665788 reply: b'250-DSN\r\n'
12:40:37.665806 reply: b'250-ENHANCEDSTATUSCODES\r\n'
12:40:37.665825 reply: b'250-STARTTLS\r\n'
12:40:37.665842 reply: b'250-8BITMIME\r\n'
12:40:37.665859 reply: b'250-BINARYMIME\r\n'
12:40:37.665876 reply: b'250-CHUNKING\r\n'
12:40:37.665893 reply: b'250 SMTPUTF8\r\n'
12:40:37.665919 reply: retcode (250); Msg: b'MWHPR20CA0007.outlook.office365.com Hello [65.54.12.68]\nSIZE 157286400\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nSTARTTLS\n8BITMIME\nBINARYMIME\nCHUNKING\nSMTPUTF8'
12:40:37.666440 send: 'STARTTLS\r\n'
12:40:37.716214 reply: b'220 2.0.0 SMTP server ready\r\n'
12:40:37.716265 reply: retcode (220); Msg: b'2.0.0 SMTP server ready'
12:40:37.878320 send: 'ehlo [10.60.69.196]\r\n'
12:40:37.928198 reply: b'250-MWHPR20CA0007.outlook.office365.com Hello [65.54.12.68]\r\n'
12:40:37.928259 reply: b'250-SIZE 157286400\r\n'
12:40:37.928285 reply: b'250-PIPELINING\r\n'
12:40:37.928304 reply: b'250-DSN\r\n'
12:40:37.928323 reply: b'250-ENHANCEDSTATUSCODES\r\n'
12:40:37.928437 reply: b'250-AUTH LOGIN XOAUTH2\r\n'
12:40:37.928461 reply: b'250-8BITMIME\r\n'
12:40:37.928494 reply: b'250-BINARYMIME\r\n'
12:40:37.928511 reply: b'250-CHUNKING\r\n'
12:40:37.928528 reply: b'250 SMTPUTF8\r\n'
12:40:37.928552 reply: retcode (250); Msg: b'MWHPR20CA0007.outlook.office365.com Hello [65.54.12.68]\nSIZE 157286400\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nAUTH LOGIN XOAUTH2\n8BITMIME\nBINARYMIME\nCHUNKING\nSMTPUTF8'
Traceback (most recent call last):
  File "mail9.py", line 18, in <module>
    smtpserver.login("", "")
  File "/aae/msio_install_release1.9/scoring_engine.20201201.3.8.3/lib/python3.8/smtplib.py", line 723, in login
    (code, resp) = self.auth(
  File "/aae/msio_install_release1.9/scoring_engine.20201201.3.8.3/lib/python3.8/smtplib.py", line 635, in auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
TypeError: can only concatenate str (not "bytes") to str
msg386594 - (view) Author: Grégory Starck (gstarck) * Date: 2021-02-07 18:00
I reproduced directly. by using a valid smtp server.
msg386597 - (view) Author: Grégory Starck (gstarck) * Date: 2021-02-07 18:09
problem seems to/could be in /usr/lib/python3.8/email/base64mime.py


```
def body_encode(s, maxlinelen=76, eol=NL):
    r"""Encode a string with base64.

    Each line will be wrapped at, at most, maxlinelen characters (defaults to
    76 characters).

    Each line of encoded text will end with eol, which defaults to "\n".  Set
    this to "\r\n" if you will be using the result of this function directly
    in an email.
    """
    if not s:
        return s
```

If function is supposed to return str for any bytes input, then there it is returning the original bytes instance.. which is the trouble. it should return `""` and that's it ?
 
I would like to work/contribute on this issue.. I'll further investigate this issue and possible solution and comes with a patch rather sooner than later..
msg386604 - (view) Author: Grégory Starck (gstarck) * Date: 2021-02-07 19:30
I filled the PR https://github.com/python/cpython/pull/24476

feel free to comment.
msg389806 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-03-30 08:37
New changeset cf35e05f89bb008d6f4553f9875e0fe87fc02406 by Grégory Starck in branch 'master':
bpo-43125: Fix: return expected type (str), not original value (bytes) in email/base64mime.py::body_encode (GH-24476)
https://github.com/python/cpython/commit/cf35e05f89bb008d6f4553f9875e0fe87fc02406
History
Date User Action Args
2021-03-30 08:38:10corona10setstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.10, - Python 3.6, Python 3.8
2021-03-30 08:37:44corona10setmessages: + msg389806
2021-02-22 00:54:13corona10setnosy: + corona10
2021-02-07 19:30:20gstarcksetmessages: + msg386604
2021-02-07 18:40:28gstarcksetkeywords: + patch
stage: patch review
pull_requests: + pull_request23268
2021-02-07 18:25:48gstarcksetversions: + Python 3.6
2021-02-07 18:09:50gstarcksetmessages: + msg386597
2021-02-07 18:00:39gstarcksetnosy: + gstarck
messages: + msg386594
2021-02-04 13:23:28boom0192create