Browse Source

Fixed #23461 -- Added EMAIL_TIMEOUT setting

José Padilla 10 years ago
parent
commit
5472d18e31

+ 1 - 0
django/conf/global_settings.py

@@ -196,6 +196,7 @@ EMAIL_USE_TLS = False
 EMAIL_USE_SSL = False
 EMAIL_USE_SSL = False
 EMAIL_SSL_CERTFILE = None
 EMAIL_SSL_CERTFILE = None
 EMAIL_SSL_KEYFILE = None
 EMAIL_SSL_KEYFILE = None
+EMAIL_TIMEOUT = None
 
 
 # List of strings representing installed apps.
 # List of strings representing installed apps.
 INSTALLED_APPS = ()
 INSTALLED_APPS = ()

+ 1 - 1
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.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_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.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_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
         self.ssl_certfile = settings.EMAIL_SSL_CERTFILE if ssl_certfile is None else ssl_certfile
         if self.use_ssl and self.use_tls:
         if self.use_ssl and self.use_tls:

+ 13 - 0
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
 :func:`python:ssl.wrap_socket` function for details on how the certificate chain
 file and private key file are handled.
 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
 .. setting:: FILE_CHARSET
 
 
 FILE_CHARSET
 FILE_CHARSET
@@ -3089,6 +3101,7 @@ Email
 * :setting:`EMAIL_SSL_CERTFILE`
 * :setting:`EMAIL_SSL_CERTFILE`
 * :setting:`EMAIL_SSL_KEYFILE`
 * :setting:`EMAIL_SSL_KEYFILE`
 * :setting:`EMAIL_SUBJECT_PREFIX`
 * :setting:`EMAIL_SUBJECT_PREFIX`
+* :setting*`EMAIL_TIMEOUT`
 * :setting:`EMAIL_USE_TLS`
 * :setting:`EMAIL_USE_TLS`
 * :setting:`MANAGERS`
 * :setting:`MANAGERS`
 * :setting:`SERVER_EMAIL`
 * :setting:`SERVER_EMAIL`

+ 3 - 0
docs/releases/1.8.txt

@@ -154,6 +154,9 @@ Email
   authentication with the :setting:`EMAIL_SSL_CERTFILE` and
   authentication with the :setting:`EMAIL_SSL_CERTFILE` and
   :setting:`EMAIL_SSL_KEYFILE` settings.
   :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
 File Storage
 ^^^^^^^^^^^^
 ^^^^^^^^^^^^
 
 

+ 11 - 25
docs/topics/email.txt

@@ -451,41 +451,27 @@ SMTP backend
     The server address and authentication credentials are set in the
     The server address and authentication credentials are set in the
     :setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`,
     :setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`,
     :setting:`EMAIL_HOST_PASSWORD`, :setting:`EMAIL_USE_TLS`,
     :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
     The SMTP backend is the default configuration inherited by Django. If you
     want to specify it explicitly, put the following in your settings::
     want to specify it explicitly, put the following in your settings::
 
 
         EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
         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
     .. 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:
 .. _topic-email-console-backend:
 
 

+ 5 - 0
tests/mail/tests.py

@@ -1033,3 +1033,8 @@ class SMTPBackendTests(BaseEmailBackendTests, SimpleTestCase):
         self.assertEqual(myemailbackend.timeout, 42)
         self.assertEqual(myemailbackend.timeout, 42)
         self.assertEqual(myemailbackend.connection.timeout, 42)
         self.assertEqual(myemailbackend.connection.timeout, 42)
         myemailbackend.close()
         myemailbackend.close()
+
+    @override_settings(EMAIL_TIMEOUT=10)
+    def test_email_timeout_override_settings(self):
+        backend = smtp.EmailBackend()
+        self.assertEqual(backend.timeout, 10)