Through the smtp email server, I'm attempting to send an email. I tried using the SMTP SSL() and starttls() methods to send emails, but both returned errors: WRONG VERSION NUMBER for SMTP SSL() and CERTIFICATE VERIFY FAILED for starttls(). Below are the given tracebacks.
Traceback of SMTP_SSL():
File "mail_sender_test.py", line 15, in <module>
with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
File "/usr/lib64/python3.6/smtplib.py", line 1031, in __init__
source_address)
File "/usr/lib64/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python3.6/smtplib.py", line 1039, in _get_socket
server_hostname=self._host)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:897)
Traceback of starttls():
File "mail_sender_test.py", line 21, in <module>
server.starttls(context=context)
File "/usr/lib64/python3.6/smtplib.py", line 771, in starttls
server_hostname=self._host)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)
Here is the sample code.
import smtplib, ssl
smtp_server = "1.2.3.4"
smtp_port = 25
smtp_password = "1234"
sender_address = "abcd@def.com"
receiver_address = "xyz@gmail.com"
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
server.login(sender_address, smtp_password)
server.sendmail(sender_address, receiver_address, message)
# with smtplib.SMTP(smtp_server, smtp_port) as server:
# server.ehlo() # Can be omitted
# server.starttls(context=context)
# server.ehlo() # Can be omitted
# server.login(sender_address, smtp_password)
# server.sendmail(sender_address, receiver_address, message)
If anyone could assist in solving the problem, that would be wonderful.
Thanks