Sending email with python 3.10 not working with error EOF occurred in violation of protocol (_ssl.c:1007)

1.5k Views Asked by At

I am trying to send an email using python 310 but am getting an error that I can't find a solution to. I should mention I am using a company proxy but this seems to work with python 3.8 so I'm not sure why it doesn't now. The code looks as such:

def send_email():
    text = self.message.as_string()
    with smtplib.SMTP(smtp_host, smtp_port) as server:
        server.ehlo()
        if server.has_extn('STARTTLS'):
            server.starttls()
            server.ehlo()
            print(f"Sending email")
            server.sendmail(sender_email, receiver_email, text)
            print('Email sent')

The error is this:

File "path\mail.py", line 102, in send_email
server.starttls()
File "path\env\lib\smtplib.py", line 790, in starttls
self.sock = context.wrap_socket(self.sock,
File "path\env\lib\ssl.py", line 513, in wrap_socket
return self.sslsocket_class._create(
File "path\env\lib\ssl.py", line 1071, in _create
self.do_handshake()
File "path\env\lib\ssl.py", line 1342, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)

I think this is linked to OPENSSL but I can't get it to work with any changes. Does anyone know how I could fix this? Thanks

Update

The source of the error is that by default python 3.8 uses a lower version of OpenSSL with has a default cipssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA') which is considered weak. I have OpenSSL 3.0.10, thus it fails. I assume the handshake is failing due to authentication and ciphers (I'm no expert on this so I may be misunderstanding).

I could try to downgrade OpenSSL to 1.1.1 which I know works but I believe this is not recommended?

2

There are 2 best solutions below

0
Islam Hossain On

I faced similar problems few days ago - May be the listed solution will work -

  • Ensure the correct SMTP port for SSL/TLS.
  • Verify that your SMTP server supports the TLS/SSL encryption method you're trying to use.
0
ashokrajar On

g3ds133 You are right root cause is due to the OpenSSL 3.

You can find the explanation here : https://devcenter.heroku.com/articles/heroku-22-stack#openssl-3 Similar bug reports on other software stack but related : https://github.com/redis/redis/issues/10915

And few possible solutions: https://stackoverflow.com/a/77177494/2018712