2

I am writing a small Python program that involves sending emails to players in a game. I'm currently using SMTPlib to do so, and for the first while it worked great, however now I get this error whenever I try to run it:

Traceback (most recent call last):
  File "/Users/user/Desktop/test.py", line 17, in <module>
    sendEmail()
  File "/Users/user/Desktop/test.py", line 11, in sendEmail
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1041, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1122)

And the bare-bones code that throws this error:

import smtplib, ssl

def sendEmail():
    port = 465  # For SSL
    password = 'password'
    senderEmail = 'senderEmail@email.com'
    
    # Create a secure SSL context
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login(senderEmail, password)
        message = 'Hello world!'
        
        server.sendmail(senderEmail, 'myEmail@email.com', message)

sendEmail()

I suspect that this error has to do with me using Python 3.9.0 instead of 3.7.6 as that is the only thing that has changed about my environment since the last time it was working, but I'm not certain about that*. What is causing this error, and what can I do to fix it?

*EDIT: I ran the same code on Python 3.7.6 in case that was the problem. Interestingly, it now gives a very similar error:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    sendEmail()
  File "test.py", line 11, in sendEmail
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1039, in _get_socket
    server_hostname=self._host)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
Catyre
  • 77
  • 7
  • If this is what you suspect, you should actually try to run your code with Python 3.7.6 and tell us if that caused it ;) – Finomnis Oct 30 '20 at 13:40
  • Edited my post to include that as an attempted solution – Catyre Oct 30 '20 at 14:32
  • Have you looked at this? https://stackoverflow.com/questions/22027418/openssl-python-requests-error-certificate-verify-failed – Finomnis Oct 30 '20 at 17:08
  • 1
    Yeah, I tried the top answer's solution, as well as `pip install certifi` but it also did not work :/ – Catyre Oct 30 '20 at 20:13

0 Answers0