This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib is broken in Python3
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.0, Python 3.1
process
Status: closed Resolution: fixed
Dependencies: 3921 5304 Superseder:
Assigned To: r.david.murray Nosy List: ajaksu2, j.l.caceres, kalevi, loewis, marcin.bachry, miwa, r.david.murray, toastedrobot
Priority: critical Keywords: easy, patch

Created on 2009-02-14 14:19 by miwa, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
smtptest.py kalevi, 2009-02-14 19:02 smtplib test file
3.0.txt miwa, 2009-02-15 07:04 Python3.0's output of smtptest.py
smtplib_eol.diff ajaksu2, 2009-04-25 22:46 Musashi's fix as a patch
test-smtplib.diff r.david.murray, 2009-05-23 14:47 unit test
Messages (12)
msg82064 - (view) Author: Musashi Tamura (miwa) Date: 2009-02-14 14:19
Issue #<3921> may be the same problem.

Sending Gmail by smtplib fails on Python 3.0 and 3.0.1.
It seems to be exist two problems in encode_plain function in smtplib.py:
  * parameter of encode_base64 must be bytes, not str,
  * by default, encode_base64 adds extra newline.
The following is an example of patch.

# original version
def encode_plain(user, password):
    return encode_base64("\0%s\0%s" % (user, password))

# fixed version. Note that "eol=''" is given in Python 2.6's smtplib.
def encode_plain(user, password):
    s = "\0%s\0%s" % (user, password)
    return encode_base64(s.encode('ascii'), eol='')
msg82065 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-02-14 14:24
Can you show us the failing application? Most likely, the bug is in your
code, not in Python.
msg82107 - (view) Author: (kalevi) Date: 2009-02-14 19:02
The attached test script works fine in Python 2.6.

Replace the following texts in the script:
from@gmail.com
to@gmail.com
password
msg82140 - (view) Author: Musashi Tamura (miwa) Date: 2009-02-15 07:04
The attachment is output of smtptest.py on Python 3.0.
msg82438 - (view) Author: bill (toastedrobot) Date: 2009-02-18 22:46
sorry, pressed the wrong button.

that solution does work. didn't find this until #python helped me get:

return encode_base64( ("\0%s\0%s" % (user, password) ).encode('ascii') )
msg86539 - (view) Author: Marcin Bachry (marcin.bachry) Date: 2009-04-25 19:27
I add simple smtp auth unit test to exercise this bug.
msg86563 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-04-25 22:46
Martin: see a test script in issue 3921.
msg88237 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-23 14:34
5304 and 3921 are fixed.  Is there still an issue here?  If so, I think
we need a test case we can add to the test suite.  It can be a patch
against the new test_smtpnet.py test.
msg88239 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-23 14:47
Looks like I accidentally deleted the file I was asking for.  Not sure
how that happened, but I'm reattaching it.
msg88253 - (view) Author: José Luis Cáceres (j.l.caceres) Date: 2009-05-23 22:34
There is a similar problem that I found with encode_cram_md5 in 
smtplib.py, SMTP.login() method. I used the solution proposed by miwa, 
both for PLAIN and CRAM MD5 authentication. Additionally, for the last 
one, I had to introduce a second correction and byte encode the 
password string when passing it to hmac.HMAC.  

I do not know if I did things correctly, but just in case it can help  
here is the complete patch that I used and worked well with the two 
AUTH methods. I keep the original and modified lines for clarity.

def encode_cram_md5(challenge, user, password):
            challenge = base64.decodestring(challenge)
            #response = user + " " + hmac.HMAC(password,    
challenge).hexdigest()
            response = user + " " + hmac.HMAC(password.encode(), 
challenge).hexdigest()
            #return encode_base64(response)
            return encode_base64((response).encode('ascii'), eol='')
            
        def encode_plain(user, password):
            #return encode_base64("\0%s\0%s" % (user, password))
            return encode_base64(("\0%s\0%s" % (user, password)).encode
('ascii'), eol='')
msg88262 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-24 14:53
Committed the simple auth tests and fix in r72868 in py3k and r72877 in
3.0.  Also added the test to trunk in r72878 to keep the test source in
sync, and to 26maint similarly in r72886.

Now we need tests for the other auth cases.
msg88473 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-28 18:51
LOGIN and CRAM-MD5 login are fixed in r72990 (3.1) and r72991 (3.0).
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49509
2010-12-27 17:04:58r.david.murrayunlinkissue1685453 dependencies
2010-01-09 17:28:58r.david.murraylinkissue6523 superseder
2009-06-23 16:21:49r.david.murraylinkissue6328 dependencies
2009-06-16 12:59:24r.david.murraylinkissue6291 superseder
2009-05-28 18:52:20r.david.murraysetstage: test needed -> resolved
2009-05-28 18:51:52r.david.murraysetstatus: open -> closed
resolution: fixed
messages: + msg88473
2009-05-24 14:53:21r.david.murraysetmessages: + msg88262
stage: commit review -> test needed
2009-05-23 22:34:34j.l.caceressetnosy: + j.l.caceres
messages: + msg88253
2009-05-23 18:21:08r.david.murraysetassignee: r.david.murray
stage: test needed -> commit review
2009-05-23 14:47:56r.david.murraysetfiles: + test-smtplib.diff

messages: + msg88239
2009-05-23 14:34:06r.david.murraysetnosy: + r.david.murray
messages: + msg88237
2009-05-23 14:24:18r.david.murraysetfiles: - test-smtplib.diff
2009-04-25 22:53:46ajaksu2linkissue5304 superseder
2009-04-25 22:46:11ajaksu2setfiles: + smtplib_eol.diff
priority: normal -> critical

nosy: + ajaksu2
messages: + msg86563
2009-04-25 22:44:26ajaksu2linkissue3921 superseder
2009-04-25 19:27:42marcin.bachrysetfiles: + test-smtplib.diff

nosy: + marcin.bachry
messages: + msg86539

keywords: + patch
2009-04-22 14:40:11ajaksu2setpriority: normal
keywords: + easy
dependencies: + smtplib cannot sendmail over TLS, email/base64mime.py cannot work
type: crash -> behavior
stage: test needed
2009-03-31 06:05:27ocean-citylinkissue1685453 dependencies
2009-02-18 22:46:16toastedrobotsetnosy: + toastedrobot
type: crash
messages: + msg82438
title: gmail smtp -> smtplib is broken in Python3
2009-02-18 22:43:23toastedrobotsettitle: smtplib is broken in Python3 -> gmail smtp
type: crash -> (no value)
versions: + Python 3.1
2009-02-15 07:04:13miwasetfiles: + 3.0.txt
messages: + msg82140
2009-02-14 19:02:38kalevisetfiles: + smtptest.py
nosy: + kalevi
messages: + msg82107
2009-02-14 14:24:02loewissetnosy: + loewis
messages: + msg82065
2009-02-14 14:19:17miwacreate