Fixed #27210 -- Allowed SMTPBackend to fail silently on a socket connection error.

This commit is contained in:
Vesteinn Snaebjarnarson 2016-09-27 18:34:49 +00:00 committed by Tim Graham
parent 0f6829a68a
commit 602bffe758
2 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,6 @@
"""SMTP email backend class.""" """SMTP email backend class."""
import smtplib import smtplib
import socket
import ssl import ssl
import threading import threading
@ -71,7 +72,7 @@ class EmailBackend(BaseEmailBackend):
if self.username and self.password: if self.username and self.password:
self.connection.login(force_str(self.username), force_str(self.password)) self.connection.login(force_str(self.username), force_str(self.password))
return True return True
except smtplib.SMTPException: except (smtplib.SMTPException, socket.error) as e:
if not self.fail_silently: if not self.fail_silently:
raise raise

View File

@ -7,6 +7,7 @@ import mimetypes
import os import os
import shutil import shutil
import smtpd import smtpd
import socket
import sys import sys
import tempfile import tempfile
import threading import threading
@ -1448,6 +1449,9 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
finally: finally:
SMTP.send = send SMTP.send = send
class SMTPNoServerTests(SimpleTestCase):
def test_send_messages_after_open_failed(self): def test_send_messages_after_open_failed(self):
""" """
send_messages() shouldn't try to send messages if open() raises an send_messages() shouldn't try to send messages if open() raises an
@ -1461,6 +1465,16 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com']) email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'])
self.assertEqual(backend.send_messages([email]), None) self.assertEqual(backend.send_messages([email]), None)
def test_fail_silently_on_connection_error(self):
"""
A socket connection error is silenced with fail_silently=True.
"""
backend = smtp.EmailBackend(username='', password='')
with self.assertRaises(socket.error):
backend.open()
backend.fail_silently = True
backend.open()
class SMTPBackendStoppedServerTest(SMTPBackendTestsBase): class SMTPBackendStoppedServerTest(SMTPBackendTestsBase):
""" """