python / cpython

The Python programming language
https://www.python.org
Other
62.33k stars 29.94k forks source link

When using an SMTP SSL connection,, get ValueError. #80275

Open 4582474d-c968-4637-81d3-838e2bd90bd4 opened 5 years ago

4582474d-c968-4637-81d3-838e2bd90bd4 commented 5 years ago
BPO 36094
Nosy @warsaw, @tiran, @bitdancer, @amcgregor, @dstufft, @csabella, @tyrone-zhao
PRs
  • python/cpython#11998
  • python/cpython#23490
  • python/cpython#23635
  • Files
  • 1.png: How does it happen
  • 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: ```python assignee = None closed_at = None created_at = labels = ['3.8', 'type-bug', '3.7', 'expert-email'] title = 'When using an SMTP SSL connection,, get ValueError.' updated_at = user = 'https://github.com/tyrone-zhao' ``` bugs.python.org fields: ```python activity = actor = 'gertburger' assignee = 'none' closed = False closed_date = None closer = None components = ['email'] creation = creator = 'tyrone-zhao' dependencies = [] files = ['48167'] hgrepos = [] issue_num = 36094 keywords = ['patch'] message_count = 4.0 messages = ['336393', '336404', '376344', '377833'] nosy_count = 10.0 nosy_names = ['barry', 'christian.heimes', 'r.david.murray', 'amcgregor', 'python-dev', 'gertburger', 'dstufft', 'cheryl.sabella', 'tyrone-zhao', 'Dadeos'] pr_nums = ['11998', '23490', '23635'] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue36094' versions = ['Python 3.7', 'Python 3.8'] ```

    4582474d-c968-4637-81d3-838e2bd90bd4 commented 5 years ago

    The following bug occurs when you connect after creating an instance of SMTP_SSL:

    import smtplib
    
    smtp_server = "smtp.163.com"
    con2 = smtplib.SMTP_SSL()
    con2.connect(smtp_server, 465)

    ValueError: server_hostname cannot be an empty string or start with a leading dot. File "E:\code\noUse.py", line 8, in \<module> con2.connect(smtp_server, 465)

    csabella commented 5 years ago

    tyrone-zhao, thank you for the report and the detail on the commit.

    Adding @christian.heimes to the nosylist as he added change this under bpo-19785 and @dstufft as he modified it to be self._host.

    0ae44e0e-ae22-4fd0-a0bf-4981b7a84801 commented 4 years ago

    I wanted to enquire as to if/when the proposed PR11998 is likely to be merged?

    I also wanted to note the similarity between this issue and bpo-41470 and to suggest that whilst the change proposed in commit e445ccbc of PR11998 [1] would be welcome, there may be a case for exposing the value ultimately passed as the server_hostname parameter to the SSLContext.wrap_socket(…) call [2] as a parameter to SMTP.connect(…) [3]. Doing so would aid potential use cases whereby the host parameter provided is an address, yet the TLS server hostname validation should be based upon another value (i.e. the hostname); potentially useful in scenarios where the host's address has been previously derived, by the caller, by other means (e.g. a call to socket.getaddrinfo(…) [4]).

    [1] https://github.com/python/cpython/pull/11998/commits/e445ccbc483dfde74638dbb694132dc00ced4973 [2] https://docs.python.org/3/library/ssl.html#ssl.SSLContext.wrap_socket [3] https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.connect [4] https://docs.python.org/3/library/socket.html#socket.getaddrinfo

    e265cfb1-40e0-4018-92d1-4ee5f1021dc6 commented 3 years ago

    Howdy! One of my projects, Marrow Mailer (formerly TurboMail) has received a bit of a flood of reports and erroneous pull requests attempting to correct this bug in the invocation of smtplib by Mailer.

    https://github.com/marrow/mailer/issues/83 is our own tracking issue, with https://github.com/marrow/mailer/pull/91 as the most "successful" pull request with more detailed (contextualized) traceback, comparison, and links to official documentation. (Also, yay, worked around the problem here by recovering my "native" issue tracker account rather than trying to sign in with GitHub.)

    This appears to be a clear regression. The particular reason why I'm unwilling to accept these patches to Mailer is that passing a hostname at SMTPSSL instantiation time will cause the connection to be initiated from within \_init__ itself, prior to any ability to set the diagnostic logging level, which is boo, hiss. Initializers actually doing things is un-good, beyond diagnostic logging not being an optional keyword argument. (Instantiation != procedural invocation.)

    Leetcore commented 1 year ago

    I have a Ubuntu 22.04.3 LTS server with Python 3.10.12 and this exact problem: SMTP_SSL(hostname) just freezes!

    I found a workaround:

    conn = SMTP()
    conn._host = hostname
    conn.connect(hostname, 587)
    conn.starttls()
    conn.login(username, password)
    conn.sendmail(sender, destination, "body")
    conn.close()
    MonsieurCellophane commented 1 month ago

    Still there in 3.11.9 (after 5 years!) Workaround mentioned in: https://stackoverflow.com/questions/51768041/python3-smtp-valueerror-server-hostname-cannot-be-an-empty-string-or-start-with#53385409