mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #19382 -- Stopped smtp backend raising exception when already closed
Thanks Sebastian Noack for the report and the initial patch.
This commit is contained in:
		@@ -60,6 +60,8 @@ class EmailBackend(BaseEmailBackend):
 | 
			
		||||
 | 
			
		||||
    def close(self):
 | 
			
		||||
        """Closes the connection to the email server."""
 | 
			
		||||
        if self.connection is None:
 | 
			
		||||
            return
 | 
			
		||||
        try:
 | 
			
		||||
            try:
 | 
			
		||||
                self.connection.quit()
 | 
			
		||||
 
 | 
			
		||||
@@ -492,6 +492,16 @@ class BaseEmailBackendTests(object):
 | 
			
		||||
        self.assertEqual(message.get('from'), "tester")
 | 
			
		||||
        self.assertEqual(message.get('to'), "django")
 | 
			
		||||
 | 
			
		||||
    def test_close_connection(self):
 | 
			
		||||
        """
 | 
			
		||||
        Test that connection can be closed (even when not explicitely opened)
 | 
			
		||||
        """
 | 
			
		||||
        conn = mail.get_connection(username='', password='')
 | 
			
		||||
        try:
 | 
			
		||||
            conn.close()
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            self.fail("close() unexpectedly raised an exception: %s" % e)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LocmemBackendTests(BaseEmailBackendTests, TestCase):
 | 
			
		||||
    email_backend = 'django.core.mail.backends.locmem.EmailBackend'
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user