Opened 19 months ago

Last modified 19 months ago

#34524 closed Bug

Error while sending TLS smtp email on "self-signed certificate" server — at Version 2

Reported by: Buky Owned by: nobody
Component: Core (Mail) Version: 4.2
Severity: Normal Keywords: ssl
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Buky)

The issue appears in Django 4.2 (tested with Python 3.11 and Python 3.9).
Correctly works with previous versions of Django: 3.2 and 4.1

Context: I use locally use the protonmail-bridge solution that locally bind an IMAP and SMTP ports with STARTTLS. Then, I use in my project config the EmailBackend to connect to the solution and send emails.

Here is the traceback, when I try to send emails with Django 4.2:

Traceback (most recent call last):
  File "/home/user/my-project/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/my-project/my-app/management/commands/send-mail.py", line 62, in handle
    send_mail(f"[My subject] {options['subject']}",
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/mail/__init__.py", line 87, in send_mail
    return mail.send()
           ^^^^^^^^^^^
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/mail/message.py", line 298, in send
    return self.get_connection(fail_silently).send_messages([self])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/mail/backends/smtp.py", line 127, in send_messages
    new_conn_created = self.open()
                       ^^^^^^^^^^^
  File "/home/user/.virtualenvs/env/lib/python3.11/site-packages/django/core/mail/backends/smtp.py", line 92, in open
    self.connection.starttls(context=self.ssl_context)
  File "/usr/lib/python3.11/smtplib.py", line 790, in starttls
    self.sock = context.wrap_socket(self.sock,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/ssl.py", line 1075, in _create
    self.do_handshake()
  File "/usr/lib/python3.11/ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:992)

I found another ticket recently created ticket related to SSL issue, but the issue doesn't exactly the same and the issue doesn't precisely mention when the patch will be merged: [4.2.x] Fixed.

I have tried to export the TLS certificate and key of the solution and add them in my settings.py like:

EMAIL_SSL_CERTFILE="/tmp/cert.pem"
EMAIL_SSL_KEYFILE="/tmp/key.pem"

And to put them in the trust store of the system:

cp /tmp/cert.pem /usr/local/share/ca-certificates/protonmail-bridge.crt
cp /tmp/key.pem /etc/ssl/private/protonmail-bridge.key
update-ca-certificates

But both tests didn't work.

I suppose ssl lib need an extra argument to allow SSL context with self-signed certificate.

Change History (2)

comment:1 by Buky, 19 months ago

Description: modified (diff)

comment:2 by Buky, 19 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top