Code

Ticket #9383: mail_admins.diff

File mail_admins.diff, 4.4 KB (added by adunar, 6 years ago)
Line 
1Index: django/core/handlers/base.py
2===================================================================
3--- django/core/handlers/base.py        (revision 9232)
4+++ django/core/handlers/base.py        (working copy)
5@@ -148,13 +148,15 @@
6             return debug.technical_500_response(request, *exc_info)
7 
8         # When DEBUG is False, send an error message to the admins.
9-        subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
10-        try:
11-            request_repr = repr(request)
12-        except:
13-            request_repr = "Request repr() unavailable"
14-        message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
15-        mail_admins(subject, message, fail_silently=True)
16+        if settings.ADMINS:
17+            subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
18+            try:
19+                request_repr = repr(request)
20+            except:
21+                request_repr = "Request repr() unavailable"
22+            message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
23+            mail_admins(subject, message, fail_silently=True)
24+           
25         # Return an HttpResponse that displays a friendly error message.
26         callback, param_dict = resolver.resolve500()
27         return callback(request, **param_dict)
28Index: django/core/mail.py
29===================================================================
30--- django/core/mail.py (revision 9232)
31+++ django/core/mail.py (working copy)
32@@ -366,12 +366,14 @@
33 
34 def mail_admins(subject, message, fail_silently=False):
35     """Sends a message to the admins, as defined by the ADMINS setting."""
36-    EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
37+    if settings.ADMINS:
38+        EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
39                  settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS]
40                  ).send(fail_silently=fail_silently)
41 
42 def mail_managers(subject, message, fail_silently=False):
43     """Sends a message to the managers, as defined by the MANAGERS setting."""
44-    EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
45+    if settings.MANAGERS:
46+        EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
47                  settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS]
48                  ).send(fail_silently=fail_silently)
49Index: tests/regressiontests/mail/tests.py
50===================================================================
51--- tests/regressiontests/mail/tests.py (revision 9232)
52+++ tests/regressiontests/mail/tests.py (working copy)
53@@ -2,7 +2,7 @@
54 r"""
55 # Tests for the django.core.mail.
56 
57->>> from django.core.mail import EmailMessage
58+>>> from django.core.mail import EmailMessage, mail_admins, mail_managers
59 >>> from django.utils.translation import ugettext_lazy
60 
61 # Test normal ascii character case:
62@@ -60,4 +60,32 @@
63 >>> email.message().as_string()
64 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent'
65 
66+# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383)
67+
68+>>> from django.core import mail
69+>>> from django.conf import settings
70+>>> old_admins = settings.ADMINS
71+>>> old_managers = settings.MANAGERS
72+>>> settings.ADMINS = []
73+>>> settings.MANAGERS = []
74+>>> mail.outbox = []
75+>>> mail_admins('hi','there')
76+>>> len(mail.outbox)
77+0
78+>>> mail.outbox = []
79+>>> mail_managers('hi','there')
80+>>> len(mail.outbox)
81+0
82+>>> settings.ADMINS = settings.MANAGERS = [('nobody','nobody@example.com')]
83+>>> mail.outbox = []
84+>>> mail_admins('hi','there')
85+>>> len(mail.outbox)
86+1
87+>>> mail.outbox = []
88+>>> mail_managers('hi','there')
89+>>> len(mail.outbox)
90+1
91+>>> settings.ADMINS = old_admins
92+>>> settings.MANAGERS = old_managers
93+
94 """
95Index: docs/topics/email.txt
96===================================================================
97--- docs/topics/email.txt       (revision 9232)
98+++ docs/topics/email.txt       (working copy)
99@@ -106,6 +106,8 @@
100 The "From:" header of the e-mail will be the value of the
101 :setting:`SERVER_EMAIL` setting.
102 
103+If :setting:`ADMINS` is empty, this function does nothing.
104+
105 This method exists for convenience and readability.
106 
107 mail_managers() function