My script
from stmplib import SMTP
con = SMTP(server, port)
con.starttls()
con.login(user, pass)
con.quit()
falls with error:
python2.7/ssl.py", line 847, in do_handshake self._sslobj.do_handshake()
When I run command openssl
to this server it falls with error 21: Verify return code: 21 (unable to verify the first certificate)
.
I would like to know how to specify in smtplib of python option “always accept self-signed certificate when connect is established via tls to e-mail server"?
Like I do in requests.get
setting key verify=False
.
Update
This variant with custom smtp class and context = ssl._create_unverified_context()
return the same error as above:
import smtplib
import ssl
class MySMTP(smtplib.SMTP):
def __init__(self, host='', port=0, timeout=5):
smtplib.SMTP.__init__(self, host, port, timeout=timeout)
self._host = host
def starttls(self, keyfile=None, certfile=None, context=None):
from urllib import _have_ssl
self.ehlo_or_helo_if_needed()
if not self.has_extn("starttls"):
raise SMTPNotSupportedError("STARTTLS extension not supported by server.")
(resp, reply) = self.docmd("STARTTLS")
if resp == 220:
if not _have_ssl:
raise RuntimeError("No SSL support included in this Python")
if context is not None and keyfile is not None:
raise ValueError("context and keyfile arguments are mutually "
"exclusive")
if context is not None and certfile is not None:
raise ValueError("context and certfile arguments are mutually "
"exclusive")
if context is None:
context = ssl._create_stdlib_context(certfile=certfile,
keyfile=keyfile)
self.sock = context.wrap_socket(self.sock,
server_hostname=self._host)
self.file = None
# RFC 3207:
# The client MUST discard any knowledge obtained from
# the server, such as the list of SMTP service extensions,
# which was not obtained from the TLS negotiation itself.
self.helo_resp = None
self.ehlo_resp = None
self.esmtp_features = {}
self.does_esmtp = 0
return (resp, reply)
con= MySMTP(server, port)
context = ssl._create_unverified_context()
con.starttls(context = context)
con.login(user, pass)
con.quit()
ssl._https_verify_certificates(enable=False)
? (although that shouldn't affect this scenario). For the 2nd variant trycontext = ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE)
(or the same thing perfrmed manually after context was created:context.verify_mode = ssl.CERT_NONE
). – Foggy