Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smtplib is broken in Python3 #49509

Closed
miwa mannequin opened this issue Feb 14, 2009 · 12 comments
Closed

smtplib is broken in Python3 #49509

miwa mannequin opened this issue Feb 14, 2009 · 12 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@miwa
Copy link
Mannequin

miwa mannequin commented Feb 14, 2009

BPO 5259
Nosy @loewis, @devdanzin, @bitdancer
Dependencies
  • bpo-3921: smtplib cannot sendmail over TLS
  • bpo-5304: email/base64mime.py cannot work
  • Files
  • smtptest.py: smtplib test file
  • 3.0.txt: Python3.0's output of smtptest.py
  • smtplib_eol.diff: Musashi's fix as a patch
  • test-smtplib.diff: unit test
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/bitdancer'
    closed_at = <Date 2009-05-28.18:51:52.597>
    created_at = <Date 2009-02-14.14:19:17.380>
    labels = ['easy', 'type-bug', 'library']
    title = 'smtplib is broken in Python3'
    updated_at = <Date 2009-05-28.18:52:20.443>
    user = 'https://bugs.python.org/miwa'

    bugs.python.org fields:

    activity = <Date 2009-05-28.18:52:20.443>
    actor = 'r.david.murray'
    assignee = 'r.david.murray'
    closed = True
    closed_date = <Date 2009-05-28.18:51:52.597>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2009-02-14.14:19:17.380>
    creator = 'miwa'
    dependencies = ['3921', '5304']
    files = ['13088', '13092', '13783', '14043']
    hgrepos = []
    issue_num = 5259
    keywords = ['patch', 'easy']
    message_count = 12.0
    messages = ['82064', '82065', '82107', '82140', '82438', '86539', '86563', '88237', '88239', '88253', '88262', '88473']
    nosy_count = 8.0
    nosy_names = ['loewis', 'ajaksu2', 'marcin.bachry', 'r.david.murray', 'miwa', 'kalevi', 'toastedrobot', 'j.l.caceres']
    pr_nums = []
    priority = 'critical'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5259'
    versions = ['Python 3.0', 'Python 3.1']

    @miwa
    Copy link
    Mannequin Author

    miwa mannequin commented Feb 14, 2009

    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='')

    @miwa miwa mannequin added type-crash A hard crash of the interpreter, possibly with a core dump stdlib Python modules in the Lib dir labels Feb 14, 2009
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Feb 14, 2009

    Can you show us the failing application? Most likely, the bug is in your
    code, not in Python.

    @kalevi
    Copy link
    Mannequin

    kalevi mannequin commented Feb 14, 2009

    The attached test script works fine in Python 2.6.

    Replace the following texts in the script:
    from@gmail.com
    to@gmail.com
    password

    @miwa
    Copy link
    Mannequin Author

    miwa mannequin commented Feb 15, 2009

    The attachment is output of smtptest.py on Python 3.0.

    @toastedrobot toastedrobot mannequin removed the type-crash A hard crash of the interpreter, possibly with a core dump label Feb 18, 2009
    @toastedrobot toastedrobot mannequin changed the title smtplib is broken in Python3 gmail smtp Feb 18, 2009
    @toastedrobot
    Copy link
    Mannequin

    toastedrobot mannequin commented Feb 18, 2009

    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') )

    @toastedrobot toastedrobot mannequin added the type-crash A hard crash of the interpreter, possibly with a core dump label Feb 18, 2009
    @toastedrobot toastedrobot mannequin changed the title gmail smtp smtplib is broken in Python3 Feb 18, 2009
    @devdanzin devdanzin mannequin added easy type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Apr 22, 2009
    @marcinbachry
    Copy link
    Mannequin

    marcinbachry mannequin commented Apr 25, 2009

    I add simple smtp auth unit test to exercise this bug.

    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Apr 25, 2009

    Martin: see a test script in bpo-3921.

    @bitdancer
    Copy link
    Member

    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.

    @bitdancer
    Copy link
    Member

    Looks like I accidentally deleted the file I was asking for. Not sure
    how that happened, but I'm reattaching it.

    @bitdancer bitdancer self-assigned this May 23, 2009
    @jlcaceres
    Copy link
    Mannequin

    jlcaceres mannequin commented May 23, 2009

    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='')

    @bitdancer
    Copy link
    Member

    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.

    @bitdancer
    Copy link
    Member

    LOGIN and CRAM-MD5 login are fixed in r72990 (3.1) and r72991 (3.0).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant