Ticket #10863: 10863.2.diff

File 10863.2.diff, 6.4 KB (added by robhudson, 5 years ago)

Updated patch to apply against trunk, added docs and tests, and API parity

  • django/core/handlers/base.py

    diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py
    index b03c2fd..10a2ac2 100644
    a b class BaseHandler(object): 
    165165            return debug.technical_500_response(request, *exc_info)
    166166
    167167        # When DEBUG is False, send an error message to the admins.
     168        from django.views.debug import ExceptionReporter
     169        reporter = ExceptionReporter(request, *exc_info)
     170        html_message = reporter.get_traceback_html()
     171
    168172        subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
    169173        try:
    170174            request_repr = repr(request)
    171175        except:
    172176            request_repr = "Request repr() unavailable"
    173177        message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
    174         mail_admins(subject, message, fail_silently=True)
     178        mail_admins(subject, message, fail_silently=True,
     179                html_message=html_message)
    175180        # If Http500 handler is not installed, re-raise last exception
    176181        if resolver.urlconf_module is None:
    177182            raise exc_info[1], None, exc_info[2]
  • django/core/mail/__init__.py

    diff --git a/django/core/mail/__init__.py b/django/core/mail/__init__.py
    index f9d1210..7c26cc8 100644
    a b def send_mass_mail(datatuple, fail_silently=False, auth_user=None, 
    8383    return connection.send_messages(messages)
    8484
    8585
    86 def mail_admins(subject, message, fail_silently=False, connection=None):
     86def mail_admins(subject, message, html_message=None,
     87        fail_silently=False, connection=None):
    8788    """Sends a message to the admins, as defined by the ADMINS setting."""
    8889    if not settings.ADMINS:
    8990        return
    90     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    91                  settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
    92                  connection=connection).send(fail_silently=fail_silently)
    93 
    94 
    95 def mail_managers(subject, message, fail_silently=False, connection=None):
     91    mail = EmailMultiAlternatives(settings.EMAIL_SUBJECT_PREFIX + subject,
     92                message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
     93                connection=connection)
     94    if html_message:
     95        mail.attach_alternative(html_message, 'text/html')
     96    mail.send(fail_silently=fail_silently)
     97
     98def mail_managers(subject, message, html_message=None,
     99        fail_silently=False, connection=None):
    96100    """Sends a message to the managers, as defined by the MANAGERS setting."""
    97101    if not settings.MANAGERS:
    98102        return
    99     EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    100                  settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
    101                  connection=connection).send(fail_silently=fail_silently)
    102 
     103    mail = EmailMultiAlternatives(settings.EMAIL_SUBJECT_PREFIX + subject,
     104                message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
     105                connection=connection)
     106    if html_message:
     107        mail.attach_alternative(html_message, 'text/html')
     108    mail.send(fail_silently=fail_silently)
    103109
    104110class SMTPConnection(_SMTPConnection):
    105111    def __init__(self, *args, **kwds):
  • docs/topics/email.txt

    diff --git a/docs/topics/email.txt b/docs/topics/email.txt
    index 31092b0..56190fe 100644
    a b mail_admins() 
    113113``django.core.mail.mail_admins()`` is a shortcut for sending an e-mail to the
    114114site admins, as defined in the :setting:`ADMINS` setting. Here's the definition:
    115115
    116     .. function:: mail_admins(subject, message, fail_silently=False, connection=None)
     116    .. function:: mail_admins(subject, message, html_message=None, fail_silently=False, connection=None)
    117117
    118118``mail_admins()`` prefixes the subject with the value of the
    119119:setting:`EMAIL_SUBJECT_PREFIX` setting, which is ``"[Django] "`` by default.
    The "From:" header of the e-mail will be the value of the 
    123123
    124124This method exists for convenience and readability.
    125125
     126.. versionchanged:: 1.3
     127
     128If ``html_message`` is provided, the resulting e-mail will be a
     129multipart/alternative e-mail with ``message`` as the "text/plain"
     130content type and ``html_message`` as the "text/html" content type.
     131
    126132mail_managers() function
    127133========================
    128134
    mail_managers() function 
    130136sends an e-mail to the site managers, as defined in the :setting:`MANAGERS`
    131137setting. Here's the definition:
    132138
    133     .. function:: mail_managers(subject, message, fail_silently=False, connection=None)
     139    .. function:: mail_managers(subject, message, html_message=None, fail_silently=False, connection=None)
    134140
    135141Examples
    136142========
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index 84be585..dc7bc41 100644
    a b Message-ID: ... 
    414414Content
    415415-------------------------------------------------------------------------------
    416416
     417# Test html_message argument to mail_admins and mail_managers
     418>>> mail_managers('Subject', 'Content', connection=connection, html_message='HTML Content')
     419Content-Type: multipart/alternative;
     420...boundary=...
     421MIME-Version: 1.0
     422Subject: [Django] Subject
     423From: root@localhost
     424To: nobody@example.com
     425Date: ...
     426Message-ID: ...
     427
     428--===============...
     429Content-Type: text/plain; charset="utf-8"
     430MIME-Version: 1.0
     431Content-Transfer-Encoding: quoted-printable
     432
     433Content
     434--===============...
     435Content-Type: text/html; charset="utf-8"
     436MIME-Version: 1.0
     437Content-Transfer-Encoding: quoted-printable
     438
     439HTML Content
     440--===============...
     441-------------------------------------------------------------------------------
     442
     443>>> mail_admins('Subject', 'Content', connection=connection, html_message='HTML Content')
     444Content-Type: multipart/alternative;
     445...boundary=...
     446MIME-Version: 1.0
     447Subject: [Django] Subject
     448From: root@localhost
     449To: nobody@example.com
     450Date: ...
     451Message-ID: ...
     452
     453--===============...
     454Content-Type: text/plain; charset="utf-8"
     455MIME-Version: 1.0
     456Content-Transfer-Encoding: quoted-printable
     457
     458Content
     459--===============...
     460Content-Type: text/html; charset="utf-8"
     461MIME-Version: 1.0
     462Content-Transfer-Encoding: quoted-printable
     463
     464HTML Content
     465--===============...
     466-------------------------------------------------------------------------------
     467
    417468>>> settings.ADMINS = old_admins
    418469>>> settings.MANAGERS = old_managers
    419470
Back to Top