mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #26210 -- Prevented SMTP backend from trying to send mail after a connection failure.
This commit is contained in:
		| @@ -41,8 +41,9 @@ class EmailBackend(BaseEmailBackend): | ||||
|  | ||||
|     def open(self): | ||||
|         """ | ||||
|         Ensures we have a connection to the email server. Returns whether or | ||||
|         not a new connection was required (True or False). | ||||
|         Ensure an open connection to the email server. Return whether or not a | ||||
|         new connection was required (True or False) or None if an exception | ||||
|         passed silently. | ||||
|         """ | ||||
|         if self.connection: | ||||
|             # Nothing to do if the connection is already open. | ||||
| @@ -102,7 +103,7 @@ class EmailBackend(BaseEmailBackend): | ||||
|             return | ||||
|         with self._lock: | ||||
|             new_conn_created = self.open() | ||||
|             if not self.connection: | ||||
|             if not self.connection or new_conn_created is None: | ||||
|                 # We failed silently on open(). | ||||
|                 # Trying to send would be pointless. | ||||
|                 return | ||||
|   | ||||
| @@ -1448,6 +1448,19 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase): | ||||
|         finally: | ||||
|             SMTP.send = send | ||||
|  | ||||
|     def test_send_messages_after_open_failed(self): | ||||
|         """ | ||||
|         send_messages() shouldn't try to send messages if open() raises an | ||||
|         exception after initializing the connection. | ||||
|         """ | ||||
|         backend = smtp.EmailBackend() | ||||
|         # Simulate connection initialization success and a subsequent | ||||
|         # connection exception. | ||||
|         backend.connection = True | ||||
|         backend.open = lambda: None | ||||
|         email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com']) | ||||
|         self.assertEqual(backend.send_messages([email]), None) | ||||
|  | ||||
|  | ||||
| class SMTPBackendStoppedServerTest(SMTPBackendTestsBase): | ||||
|     """ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user