Skip to content

smtplib is broken in Python3 #49509

Closed
Closed

Description

@miwa
mannequin
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']

    Activity

    1. miwa commented on Feb 14, 2009

      miwamannequin
      MannequinAuthor

      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='')
    2. added
      type-crashA hard crash of the interpreter, possibly with a core dump
      stdlibStandard Library Python modules in the Lib/ directory
      on Feb 14, 2009
    3. loewis commented on Feb 14, 2009

      loewismannequin
      Mannequin

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

    4. kalevi commented on Feb 14, 2009

      kalevimannequin
      Mannequin

      The attached test script works fine in Python 2.6.

      Replace the following texts in the script:
      [email protected]
      [email protected]
      password

    5. miwa commented on Feb 15, 2009

      miwamannequin
      MannequinAuthor

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

    6. removed
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Feb 18, 2009
    7. changed the title [-]smtplib is broken in Python3[/-] [+]gmail smtp[/+] on Feb 18, 2009
    8. toastedrobot commented on Feb 18, 2009

      toastedrobotmannequin
      Mannequin

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

    9. added
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Feb 18, 2009
    10. changed the title [-]gmail smtp[/-] [+]smtplib is broken in Python3[/+] on Feb 18, 2009
    11. added
      type-bugAn unexpected behavior, bug, or error
      and removed
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Apr 22, 2009
    12. marcinbachry commented on Apr 25, 2009

      marcinbachrymannequin
      Mannequin

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

    13. devdanzin commented on Apr 25, 2009

      devdanzinmannequin
      Mannequin

      Martin: see a test script in bpo-3921.

    14. bitdancer commented on May 23, 2009

      @bitdancer
      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.

    15. bitdancer commented on May 23, 2009

      @bitdancer
      Member

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

    16. self-assigned this
      on May 23, 2009
    17. jlcaceres commented on May 23, 2009

      jlcaceresmannequin
      Mannequin

      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='')
    18. bitdancer commented on May 24, 2009

      @bitdancer
      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.

    19. bitdancer commented on May 28, 2009

      @bitdancer
      Member

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

    20. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    easystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions