Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 SMTP ValueError: server_hostname cannot be an empty string or start with a leading dot

import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.gmail.com','587')
(220, b'smtp.gmail.com ESMTP h15-v6sm187291iog.48 - gsmtp')
smtp.starttls()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 771, in starttls
    server_hostname=self._host)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 843, in _create
    owner=self, session=self._session,
ValueError: server_hostname cannot be an empty string or start with a leading dot.

I am using python 3.7 on Mac and cannot perform a tls handshake with gmail. This code works on python 2.7, and on my Ubuntu server using python 3.5.2. Does anyone know what could be causing this error?

like image 939
Patrick E Avatar asked Aug 09 '18 13:08

Patrick E


1 Answers

It seems a python3.7 bug, pass the hostname to smtplib.SMTP will fix it

smtp = smtplib.SMTP('smtp.gmail.com')
smtp.connect('smtp.gmail.com','587')
like image 157
tao zhang Avatar answered Oct 08 '22 20:10

tao zhang