cardinal_pythonlib.django.mail


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


E-mail backend for Django that fixes a TLS bug.

class cardinal_pythonlib.django.mail.SmtpEmailBackendTls1(*args, **kwargs)[source]

Overrides django.core.mail.backends.smtp.EmailBackend to require TLS v1. Use this if your existing TLS server gives the error:

ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:600)

… which appears to be a manifestation of changes in Python’s smtplib library, which relies on its ssl library, which relies on OpenSSL. Something here has changed and now some servers that only support TLS version 1.0 don’t work. In these situations, the following code fails:

import smtplib
s = smtplib.SMTP(host, port)  # port typically 587
print(s.help())  # so we know we're communicating
s.ehlo()  # ditto
s.starttls()  # fails with ssl.SSLEOFError as above

and this works:

import smtplib
import ssl
s = smtplib.SMTP(host, port)
c = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.ehlo()
s.starttls(context=c)  # works

then to send a simple message:

s.login(user, password)
s.sendmail(sender, recipient, message)
open() bool[source]

Ensures we have a connection to the email server. Returns whether or not a new connection was required (True or False).