Browse Source

Improved connection clean-up for SMTP backend.

Carlton Gibson 2 years ago
parent
commit
8a844e761d
1 changed files with 8 additions and 6 deletions
  1. 8 6
      django/core/mail/backends/smtp.py

+ 8 - 6
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):