Fixed #9383 -- Don't open a network connection for sending email if there'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
This commit is contained in:
parent
62c3a7a625
commit
e3aa9a2828
1
AUTHORS
1
AUTHORS
|
@ -423,6 +423,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Jason Yan <tailofthesun@gmail.com>
|
Jason Yan <tailofthesun@gmail.com>
|
||||||
ye7cakf02@sneakemail.com
|
ye7cakf02@sneakemail.com
|
||||||
ymasuda@ethercube.com
|
ymasuda@ethercube.com
|
||||||
|
Jesse Young <adunar@gmail.com>
|
||||||
Jarek Zgoda <jarek.zgoda@gmail.com>
|
Jarek Zgoda <jarek.zgoda@gmail.com>
|
||||||
Cheng Zhang
|
Cheng Zhang
|
||||||
|
|
||||||
|
|
|
@ -268,6 +268,10 @@ class EmailMessage(object):
|
||||||
|
|
||||||
def send(self, fail_silently=False):
|
def send(self, fail_silently=False):
|
||||||
"""Sends the email message."""
|
"""Sends the email message."""
|
||||||
|
if not self.recipients():
|
||||||
|
# Don't bother creating the network connection if there's nobody to
|
||||||
|
# send to.
|
||||||
|
return 0
|
||||||
return self.get_connection(fail_silently).send_messages([self])
|
return self.get_connection(fail_silently).send_messages([self])
|
||||||
|
|
||||||
def attach(self, filename=None, content=None, mimetype=None):
|
def attach(self, filename=None, content=None, mimetype=None):
|
||||||
|
@ -366,12 +370,16 @@ def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
|
||||||
|
|
||||||
def mail_admins(subject, message, fail_silently=False):
|
def mail_admins(subject, message, fail_silently=False):
|
||||||
"""Sends a message to the admins, as defined by the ADMINS setting."""
|
"""Sends a message to the admins, as defined by the ADMINS setting."""
|
||||||
|
if not settings.ADMINS:
|
||||||
|
return
|
||||||
EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
|
EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
|
||||||
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS]
|
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS]
|
||||||
).send(fail_silently=fail_silently)
|
).send(fail_silently=fail_silently)
|
||||||
|
|
||||||
def mail_managers(subject, message, fail_silently=False):
|
def mail_managers(subject, message, fail_silently=False):
|
||||||
"""Sends a message to the managers, as defined by the MANAGERS setting."""
|
"""Sends a message to the managers, as defined by the MANAGERS setting."""
|
||||||
|
if not settings.MANAGERS:
|
||||||
|
return
|
||||||
EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
|
EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
|
||||||
settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS]
|
settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS]
|
||||||
).send(fail_silently=fail_silently)
|
).send(fail_silently=fail_silently)
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
r"""
|
r"""
|
||||||
# Tests for the django.core.mail.
|
# Tests for the django.core.mail.
|
||||||
|
|
||||||
>>> from django.core.mail import EmailMessage
|
>>> from django.conf import settings
|
||||||
|
>>> from django.core import mail
|
||||||
|
>>> from django.core.mail import EmailMessage, mail_admins, mail_managers
|
||||||
>>> from django.utils.translation import ugettext_lazy
|
>>> from django.utils.translation import ugettext_lazy
|
||||||
|
|
||||||
# Test normal ascii character case:
|
# Test normal ascii character case:
|
||||||
|
@ -60,4 +62,30 @@ BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T
|
||||||
>>> email.message().as_string()
|
>>> email.message().as_string()
|
||||||
'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'
|
'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'
|
||||||
|
|
||||||
|
# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383)
|
||||||
|
|
||||||
|
>>> old_admins = settings.ADMINS
|
||||||
|
>>> old_managers = settings.MANAGERS
|
||||||
|
>>> settings.ADMINS = []
|
||||||
|
>>> settings.MANAGERS = []
|
||||||
|
>>> mail.outbox = []
|
||||||
|
>>> mail_admins('hi','there')
|
||||||
|
>>> len(mail.outbox)
|
||||||
|
0
|
||||||
|
>>> mail.outbox = []
|
||||||
|
>>> mail_managers('hi','there')
|
||||||
|
>>> len(mail.outbox)
|
||||||
|
0
|
||||||
|
>>> settings.ADMINS = settings.MANAGERS = [('nobody','nobody@example.com')]
|
||||||
|
>>> mail.outbox = []
|
||||||
|
>>> mail_admins('hi','there')
|
||||||
|
>>> len(mail.outbox)
|
||||||
|
1
|
||||||
|
>>> mail.outbox = []
|
||||||
|
>>> mail_managers('hi','there')
|
||||||
|
>>> len(mail.outbox)
|
||||||
|
1
|
||||||
|
>>> settings.ADMINS = old_admins
|
||||||
|
>>> settings.MANAGERS = old_managers
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue