Code

Ticket #6989: mail-local-hostname2.diff

File mail-local-hostname2.diff, 3.9 KB (added by synack, 3 years ago)
Line 
1diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
2index 97b7ac6..7da8fa6 100644
3--- a/django/conf/global_settings.py
4+++ b/django/conf/global_settings.py
5@@ -176,6 +176,9 @@ EMAIL_HOST_USER = ''
6 EMAIL_HOST_PASSWORD = ''
7 EMAIL_USE_TLS = False
8 
9+# Hostname to send in the EHLO message to the SMTP server.
10+EMAIL_LOCAL_HOSTNAME = ''
11+
12 # List of strings representing installed apps.
13 INSTALLED_APPS = ()
14 
15diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
16index bb184ab..9b7304d 100644
17--- a/django/core/mail/backends/smtp.py
18+++ b/django/core/mail/backends/smtp.py
19@@ -14,12 +14,20 @@ class EmailBackend(BaseEmailBackend):
20     A wrapper that manages the SMTP network connection.
21     """
22     def __init__(self, host=None, port=None, username=None, password=None,
23-                 use_tls=None, fail_silently=False, **kwargs):
24+                 use_tls=None, fail_silently=False, from_host=None, **kwargs):
25         super(EmailBackend, self).__init__(fail_silently=fail_silently)
26         self.host = host or settings.EMAIL_HOST
27         self.port = port or settings.EMAIL_PORT
28         self.username = username or settings.EMAIL_HOST_USER
29         self.password = password or settings.EMAIL_HOST_PASSWORD
30+
31+        # If from_host and settings.EMAIL_FROM_HOST are not specified,
32+        # socket.getfqdn() gets used. For performance, we use the cached
33+        # FQDN for from_host.
34+        self.from_host = from_host or \
35+                         settings.EMAIL_LOCAL_HOSTNAME or \
36+                         DNS_NAME.get_fqdn()
37+
38         if use_tls is None:
39             self.use_tls = settings.EMAIL_USE_TLS
40         else:
41@@ -36,10 +44,8 @@ class EmailBackend(BaseEmailBackend):
42             # Nothing to do if the connection is already open.
43             return False
44         try:
45-            # If local_hostname is not specified, socket.getfqdn() gets used.
46-            # For performance, we use the cached FQDN for local_hostname.
47             self.connection = smtplib.SMTP(self.host, self.port,
48-                                           local_hostname=DNS_NAME.get_fqdn())
49+                                           local_hostname=self.from_host)
50             if self.use_tls:
51                 self.connection.ehlo()
52                 self.connection.starttls()
53diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
54index 8bf0a4e..47e54d4 100644
55--- a/docs/ref/settings.txt
56+++ b/docs/ref/settings.txt
57@@ -882,6 +882,14 @@ Whether to use a TLS (secure) connection when talking to the SMTP server.
58 
59 .. setting:: FILE_CHARSET
60 
61+EMAIL_LOCAL_HOSTNAME
62+--------------------
63+
64+Default: ``''`` (Empty string)
65+
66+The hostname to send to the SMTP server in the EHLO command. If unset, a
67+cached local DNS name from socket.getfqdn() is used.
68+
69 FILE_CHARSET
70 ------------
71 
72diff --git a/docs/topics/email.txt b/docs/topics/email.txt
73index ea62120..892ab23 100644
74--- a/docs/topics/email.txt
75+++ b/docs/topics/email.txt
76@@ -30,6 +30,8 @@ Mail is sent using the SMTP host and port specified in the
77 :setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD` settings, if
78 set, are used to authenticate to the SMTP server, and the
79 :setting:`EMAIL_USE_TLS` setting controls whether a secure connection is used.
80+:setting:`EMAIL_LOCAL_HOSTNAME` specifies the hostname to be sent in the
81+SMTP EHLO command
82 
83 .. note::
84 
85@@ -417,8 +419,8 @@ SMTP backend
86 This is the default backend. E-mail will be sent through a SMTP server.
87 The server address and authentication credentials are set in the
88 :setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`,
89-:setting:`EMAIL_HOST_PASSWORD` and :setting:`EMAIL_USE_TLS` settings in your
90-settings file.
91+:setting:`EMAIL_LOCAL_HOSTNAME`, :setting:`EMAIL_HOST_PASSWORD` and
92+:setting:`EMAIL_USE_TLS` settings in your settings file.
93 
94 The SMTP backend is the default configuration inherited by Django. If you
95 want to specify it explicitly, put the following in your settings::