Ticket #9383: mail_admins.diff

File mail_admins.diff, 4.4 KB (added by Jesse Young, 16 years ago)
  • django/core/handlers/base.py

     
    148148            return debug.technical_500_response(request, *exc_info)
    149149
    150150        # When DEBUG is False, send an error message to the admins.
    151         subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
    152         try:
    153             request_repr = repr(request)
    154         except:
    155             request_repr = "Request repr() unavailable"
    156         message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
    157         mail_admins(subject, message, fail_silently=True)
     151        if settings.ADMINS:
     152            subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
     153            try:
     154                request_repr = repr(request)
     155            except:
     156                request_repr = "Request repr() unavailable"
     157            message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
     158            mail_admins(subject, message, fail_silently=True)
     159           
    158160        # Return an HttpResponse that displays a friendly error message.
    159161        callback, param_dict = resolver.resolve500()
    160162        return callback(request, **param_dict)
  • django/core/mail.py

     
    366366
    367367def mail_admins(subject, message, fail_silently=False):
    368368    """Sends a message to the admins, as defined by the ADMINS setting."""
    369     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
     369    if settings.ADMINS:
     370        EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    370371                 settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS]
    371372                 ).send(fail_silently=fail_silently)
    372373
    373374def mail_managers(subject, message, fail_silently=False):
    374375    """Sends a message to the managers, as defined by the MANAGERS setting."""
    375     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
     376    if settings.MANAGERS:
     377        EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    376378                 settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS]
    377379                 ).send(fail_silently=fail_silently)
  • tests/regressiontests/mail/tests.py

     
    22r"""
    33# Tests for the django.core.mail.
    44
    5 >>> from django.core.mail import EmailMessage
     5>>> from django.core.mail import EmailMessage, mail_admins, mail_managers
    66>>> from django.utils.translation import ugettext_lazy
    77
    88# Test normal ascii character case:
     
    6060>>> email.message().as_string()
    6161'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'
    6262
     63# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383)
     64
     65>>> from django.core import mail
     66>>> from django.conf import settings
     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)
     740
     75>>> mail.outbox = []
     76>>> mail_managers('hi','there')
     77>>> len(mail.outbox)
     780
     79>>> settings.ADMINS = settings.MANAGERS = [('nobody','nobody@example.com')]
     80>>> mail.outbox = []
     81>>> mail_admins('hi','there')
     82>>> len(mail.outbox)
     831
     84>>> mail.outbox = []
     85>>> mail_managers('hi','there')
     86>>> len(mail.outbox)
     871
     88>>> settings.ADMINS = old_admins
     89>>> settings.MANAGERS = old_managers
     90
    6391"""
  • docs/topics/email.txt

     
    106106The "From:" header of the e-mail will be the value of the
    107107:setting:`SERVER_EMAIL` setting.
    108108
     109If :setting:`ADMINS` is empty, this function does nothing.
     110
    109111This method exists for convenience and readability.
    110112
    111113mail_managers() function
Back to Top