Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9383 -- Don't open a network connection for sending email if t…

…here's

nothing to send. Saves a bit of time when, for example, processing 500-error
emails with no ADMINs configured. Based on a patch from Jesse Young.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@9248 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e3aa9a28288737a75f05f2174b4105d01e52af96 1 parent 62c3a7a
Malcolm Tredinnick authored October 24, 2008
1  AUTHORS
@@ -423,6 +423,7 @@ answer newbie questions, and generally made Django that much better:
423 423
     Jason Yan <tailofthesun@gmail.com>
424 424
     ye7cakf02@sneakemail.com
425 425
     ymasuda@ethercube.com
  426
+    Jesse Young <adunar@gmail.com>
426 427
     Jarek Zgoda <jarek.zgoda@gmail.com>
427 428
     Cheng Zhang
428 429
 
8  django/core/mail.py
@@ -268,6 +268,10 @@ def recipients(self):
268 268
 
269 269
     def send(self, fail_silently=False):
270 270
         """Sends the email message."""
  271
+        if not self.recipients():
  272
+            # Don't bother creating the network connection if there's nobody to
  273
+            # send to.
  274
+            return 0
271 275
         return self.get_connection(fail_silently).send_messages([self])
272 276
 
273 277
     def attach(self, filename=None, content=None, mimetype=None):
@@ -366,12 +370,16 @@ def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
366 370
 
367 371
 def mail_admins(subject, message, fail_silently=False):
368 372
     """Sends a message to the admins, as defined by the ADMINS setting."""
  373
+    if not settings.ADMINS:
  374
+        return
369 375
     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
370 376
                  settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS]
371 377
                  ).send(fail_silently=fail_silently)
372 378
 
373 379
 def mail_managers(subject, message, fail_silently=False):
374 380
     """Sends a message to the managers, as defined by the MANAGERS setting."""
  381
+    if not settings.MANAGERS:
  382
+        return
375 383
     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
376 384
                  settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS]
377 385
                  ).send(fail_silently=fail_silently)
30  tests/regressiontests/mail/tests.py
@@ -2,7 +2,9 @@
2 2
 r"""
3 3
 # Tests for the django.core.mail.
4 4
 
5  
->>> from django.core.mail import EmailMessage
  5
+>>> from django.conf import settings
  6
+>>> from django.core import mail
  7
+>>> from django.core.mail import EmailMessage, mail_admins, mail_managers
6 8
 >>> from django.utils.translation import ugettext_lazy
7 9
 
8 10
 # Test normal ascii character case:
@@ -60,4 +62,30 @@
60 62
 >>> email.message().as_string()
61 63
 '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'
62 64
 
  65
+# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383)
  66
+
  67
+>>> old_admins = settings.ADMINS
  68
+>>> old_managers = settings.MANAGERS
  69
+>>> settings.ADMINS = []
  70
+>>> settings.MANAGERS = []
  71
+>>> mail.outbox = []
  72
+>>> mail_admins('hi','there')
  73
+>>> len(mail.outbox)
  74
+0
  75
+>>> mail.outbox = []
  76
+>>> mail_managers('hi','there')
  77
+>>> len(mail.outbox)
  78
+0
  79
+>>> settings.ADMINS = settings.MANAGERS = [('nobody','nobody@example.com')]
  80
+>>> mail.outbox = []
  81
+>>> mail_admins('hi','there')
  82
+>>> len(mail.outbox)
  83
+1
  84
+>>> mail.outbox = []
  85
+>>> mail_managers('hi','there')
  86
+>>> len(mail.outbox)
  87
+1
  88
+>>> settings.ADMINS = old_admins
  89
+>>> settings.MANAGERS = old_managers
  90
+
63 91
 """

0 notes on commit e3aa9a2

Please sign in to comment.
Something went wrong with that request. Please try again.