Skip to content

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

Closed as duplicate
Closed as duplicate

Description

@tyrone-zhao
BPO 36094
Nosy @warsaw, @tiran, @bitdancer, @amcgregor, @dstufft, @csabella, @Tyrone-Zhao
PRs
  • bpo-36094: Fix a bug in smtplib module #11998
  • bpo-11998: Patch smtplib.py #23490
  • bpo-36094: Update smtplib.py #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:

    assignee = None
    closed_at = None
    created_at = <Date 2019-02-23.17:44:01.948>
    labels = ['3.8', 'type-bug', '3.7', 'expert-email']
    title = 'When using an SMTP SSL connection,, get ValueError.'
    updated_at = <Date 2021-02-24.05:51:21.028>
    user = 'https://github.com/tyrone-zhao'

    bugs.python.org fields:

    activity = <Date 2021-02-24.05:51:21.028>
    actor = 'gertburger'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['email']
    creation = <Date 2019-02-23.17:44:01.948>
    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']

    Activity

    1. tyrone-zhao commented on Feb 23, 2019

      tyrone-zhaomannequin
      MannequinAuthor

      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)

    2. added
      type-bugAn unexpected behavior, bug, or error
      and removed
      performancePerformance or resource usage
      on Feb 23, 2019
    3. csabella commented on Feb 23, 2019

      @csabella
      Contributor

      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.

    4. Dadeos commented on Sep 4, 2020

      Dadeosmannequin
      Mannequin

      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] e445ccb
      [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

    5. amcgregor commented on Oct 2, 2020

      amcgregormannequin
      Mannequin

      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.

      marrow/mailer#83 is our own tracking issue, with marrow/mailer#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 SMTP_SSL 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.)

    6. transferred this issue fromon Apr 10, 2022
    7. Leetcore commented on Aug 29, 2023

      @Leetcore

      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()
      
    8. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 23, 2023
    9. MonsieurCellophane commented on Aug 8, 2024

      @MonsieurCellophane
    10. bitdancer commented on Apr 8, 2026

      @bitdancer
      Member

      #115259 has been merged to fix this issue. server_hostname as a separate parameter would be a feature request and should go in a new issue. if you see any problems with the committed fix, please comment there.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions