#34504: SSLCertVerificationError on outgoing emails for some mailboxes
-------------------------------------+-------------------------------------
     Reporter:  Kamen Kalchev        |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Core (Mail)          |                  Version:  4.2
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  smtplib, ssl,        |             Triage Stage:
  Django4.2                          |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Kamen Kalchev):

 Hello and thank you for the prompt answer, Mariusz.

 We have played around with your suggestion and what worked in our case was
 adding **check_hostname = False** and **verify_mode = ssl.CERT_NONE** in
 the else clause (since we are not passing in a specific cert/ key file) in
 an overridden **ssl_context** method for a child class of
 **EmailBackend**.

 To be honest, we are not sure if this should be changed for everyone but
 it looks like something was omitted in creating the default ssl context,
 as in Django version 4.1.7 we did not have to manually set those params
 and it worked. Anyway, we really appreciate your response and wish you a
 good week ahead.

 {{{
     @cached_property
     def ssl_context(self):
         if self.ssl_certfile or self.ssl_keyfile:
             ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
             ssl_context.load_cert_chain(self.ssl_certfile,
 self.ssl_keyfile)
             return ssl_context
         else:
             ssl_context = ssl.create_default_context()
             ssl_context.check_hostname = False
             ssl_context.verify_mode = ssl.CERT_NONE
             return ssl_context
 }}}


 Replying to [comment:2 Mariusz Felisiak]:
 > Thanks for the ticket, however I don't see much difference between the
 current implementation and Python < 3.12 behavior in creating a default
 SSL context. The only difference is that now `check_hostname` is set to
 `True`, does it work for you with the following diff?
 > {{{#!diff
 > diff --git a/django/core/mail/backends/smtp.py
 b/django/core/mail/backends/smtp.py
 > index 1ee48269ae..132bed29be 100644
 > --- a/django/core/mail/backends/smtp.py
 > +++ b/django/core/mail/backends/smtp.py
 > @@ -60,6 +60,7 @@ class EmailBackend(BaseEmailBackend):
 >          if self.ssl_certfile or self.ssl_keyfile:
 >              ssl_context =
 ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
 >              ssl_context.load_cert_chain(self.ssl_certfile,
 self.ssl_keyfile)
 > +            ssl_context.check_hostname = False
 >              return ssl_context
 >          else:
 >              return ssl.create_default_context()
 > }}}
 >
 > I'm not sure we'd like to change that.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34504#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018799df66cd-0cc061f1-66be-4019-b6be-87b72b1950b5-000000%40eu-central-1.amazonses.com.

Reply via email to