diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 186d77f3144..5fd90293258 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -196,6 +196,7 @@ EMAIL_USE_TLS = False EMAIL_USE_SSL = False EMAIL_SSL_CERTFILE = None EMAIL_SSL_KEYFILE = None +EMAIL_TIMEOUT = None # List of strings representing installed apps. INSTALLED_APPS = () diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py index b3b23027f12..4c41eb8bd61 100644 --- a/django/core/mail/backends/smtp.py +++ b/django/core/mail/backends/smtp.py @@ -24,7 +24,7 @@ class EmailBackend(BaseEmailBackend): self.password = settings.EMAIL_HOST_PASSWORD if password is None else password self.use_tls = settings.EMAIL_USE_TLS if use_tls is None else use_tls self.use_ssl = settings.EMAIL_USE_SSL if use_ssl is None else use_ssl - self.timeout = timeout + self.timeout = settings.EMAIL_TIMEOUT if timeout is None else timeout self.ssl_keyfile = settings.EMAIL_SSL_KEYFILE if ssl_keyfile is None else ssl_keyfile self.ssl_certfile = settings.EMAIL_SSL_CERTFILE if ssl_certfile is None else ssl_certfile if self.use_ssl and self.use_tls: diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 507f3f11c9a..c1d094b629b 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -1266,6 +1266,18 @@ connection. Please refer to the documentation of Python's :func:`python:ssl.wrap_socket` function for details on how the certificate chain file and private key file are handled. +.. setting:: EMAIL_TIMEOUT + +EMAIL_TIMEOUT +------------- + +.. versionadded:: 1.8 + +Default: ``None`` + +Specifies a timeout in seconds for blocking operations like the connection +attempt. + .. setting:: FILE_CHARSET FILE_CHARSET @@ -3089,6 +3101,7 @@ Email * :setting:`EMAIL_SSL_CERTFILE` * :setting:`EMAIL_SSL_KEYFILE` * :setting:`EMAIL_SUBJECT_PREFIX` +* :setting*`EMAIL_TIMEOUT` * :setting:`EMAIL_USE_TLS` * :setting:`MANAGERS` * :setting:`SERVER_EMAIL` diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 41d029a9ca2..a904f0fd138 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -154,6 +154,9 @@ Email authentication with the :setting:`EMAIL_SSL_CERTFILE` and :setting:`EMAIL_SSL_KEYFILE` settings. +* The SMTP :class:`~django.core.mail.backends.smtp.EmailBackend` now supports + setting the ``timeout`` parameter with the :setting:`EMAIL_TIMEOUT` setting. + File Storage ^^^^^^^^^^^^ diff --git a/docs/topics/email.txt b/docs/topics/email.txt index a7a93299745..7303a6fe6f9 100644 --- a/docs/topics/email.txt +++ b/docs/topics/email.txt @@ -451,41 +451,27 @@ SMTP backend The server address and authentication credentials are set in the :setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`, :setting:`EMAIL_HOST_PASSWORD`, :setting:`EMAIL_USE_TLS`, - :setting:`EMAIL_USE_SSL`, :setting:`EMAIL_SSL_CERTFILE` and - :setting:`EMAIL_SSL_KEYFILE` settings in your settings file. + :setting:`EMAIL_USE_SSL`, :setting:`EMAIL_TIMEOUT`, + :setting:`EMAIL_SSL_CERTFILE` and :setting:`EMAIL_SSL_KEYFILE` settings + in your settings file. The SMTP backend is the default configuration inherited by Django. If you want to specify it explicitly, put the following in your settings:: EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' - Here is an attribute which doesn't have a corresponding setting like the - others described above: + .. versionadded:: 1.7 - .. attribute:: timeout - - .. versionadded:: 1.7 - - This backend contains a ``timeout`` parameter, which can be set with - the following sample code:: - - from django.core.mail.backends import smtp - - class MyEmailBackend(smtp.EmailBackend): - def __init__(self, *args, **kwargs): - kwargs.setdefault('timeout', 42) - super(MyEmailBackend, self).__init__(*args, **kwargs) - - Then point the :setting:`EMAIL_BACKEND` setting at your custom backend as - described above. - - If unspecified, the default ``timeout`` will be the one provided by - :func:`socket.getdefaulttimeout()`, which defaults to ``None`` (no timeout). + The ``timeout`` parameter was added. If unspecified, the default + ``timeout`` will be the one provided by + :func:`socket.getdefaulttimeout()`, which defaults to ``None`` (no + timeout). .. versionchanged:: 1.8 - The ``ssl_keyfile`` and ``ssl_certfile`` parameters and - corresponding settings were added. + The ``ssl_keyfile``, and ``ssl_certfile`` parameters and corresponding + settings were added. The ability to customize ``timeout`` using + a setting (:setting:`EMAIL_TIMEOUT`) was added. .. _topic-email-console-backend: diff --git a/tests/mail/tests.py b/tests/mail/tests.py index cc5e3d81979..df6daee5c81 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -1033,3 +1033,8 @@ class SMTPBackendTests(BaseEmailBackendTests, SimpleTestCase): self.assertEqual(myemailbackend.timeout, 42) self.assertEqual(myemailbackend.connection.timeout, 42) myemailbackend.close() + + @override_settings(EMAIL_TIMEOUT=10) + def test_email_timeout_override_settings(self): + backend = smtp.EmailBackend() + self.assertEqual(backend.timeout, 10)