Im trying to use python to build an email sender. Ive been getting an SSL certificate error, though I downloaded it. Im also getting an SMTP error, which i think shouldn't be happening. I've tried to fix this error but the solution I found was that of Mac and not windows. The error:
with smtplib.SMTP_SSL( 'smtp.gmail.com' , 465, context=context) as smtp:
File "C:\Program Files\Inkscape\lib\python3.10\smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "C:\Program Files\Inkscape\lib\python3.10\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "C:\Program Files\Inkscape\lib\python3.10\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Program Files\Inkscape\lib\python3.10\smtplib.py", line 1057, in _get_socket
new_socket = self.context.wrap_socket(new_socket,
File "C:\Program Files\Inkscape\lib\python3.10\ssl.py", line 513, in wrap_socket
return self.sslsocket_class._create(
File "C:\Program Files\Inkscape\lib\python3.10\ssl.py", line 1071, in _create
self.do_handshake()
File "C:\Program Files\Inkscape\lib\python3.10\ssl.py", line 1342, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)
The code:
from email.message import EmailMessage
from emailApp import password
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
import smtplib
email_sender = 'example2@mail.com'
email_password = password
email_receiver = 'example@gmail.com'
subject = "msg"
body = """
Hello
"""
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject'] = subject
em.set_content(body)
context = ssl.create_default_context()
context = ssl.create_default_context()
with smtplib.SMTP_SSL( 'smtp.gmail.com' , 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, em.as_string())