summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2023-03-14 11:10:06 +0100
committerGitHub <noreply@github.com>2023-03-14 11:10:06 +0100
commit8a844e761d098d4005725f991a5e120a1f17cb70 (patch)
tree7868bf51819782dc22a999cdb512c7ef9872cddc /django
parentde0c7744be93800b2239eb061a2d8f7c669c05a8 (diff)
downloaddjango-8a844e761d098d4005725f991a5e120a1f17cb70.tar.gz
Improved connection clean-up for SMTP backend.
Diffstat (limited to 'django')
-rw-r--r--django/core/mail/backends/smtp.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
index c7ba06cde2..1ee48269ae 100644
--- a/django/core/mail/backends/smtp.py
+++ b/django/core/mail/backends/smtp.py
@@ -130,12 +130,14 @@ class EmailBackend(BaseEmailBackend):
# Trying to send would be pointless.
return 0
num_sent = 0
- for message in email_messages:
- sent = self._send(message)
- if sent:
- num_sent += 1
- if new_conn_created:
- self.close()
+ try:
+ for message in email_messages:
+ sent = self._send(message)
+ if sent:
+ num_sent += 1
+ finally:
+ if new_conn_created:
+ self.close()
return num_sent
def _send(self, email_message):