Opened 11 years ago

Closed 11 years ago

#20369 closed Bug (duplicate)

Django 1.5.1: Sending HTML mails with Unicode string contents fails

Reported by: Carsten Fuchs Owned by: nobody
Component: Core (Mail) Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A while after upgrading from Django 1.4 to Django 1.5.1 (using Python 2.6.5), I realized that I no longer got any of the automatic e-mails that are normally sent when an error occurs.

Then problem occurs only when HTML emails are sent, sending plain-text emails works without problems.

In settings.py, my LOGGING configuration is:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            # 'include_html': True,  # Doesn't work with Django 1.5.1.
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

That is, emails are reliably received when include_html is set to False, but not when at True, which used to work with Django 1.4.

At the management shell, with Django 1.5.1:

>>> from django.core.mail import mail_admins

# No Umlaut-characters --> ok.
>>> mail_admins(u"Test 1", u"Message Body", html_message=u"<h1>Ueberschrift</h1>")

# Umlaut-character in the subject --> ok.
>>> mail_admins(u"Täst 2", u"Message Body", html_message=u"<h1>Ueberschrift</h1>")

# Umlaut-character in the message body --> error.
>>> mail_admins(u"Test 3", u"Mässage Body", html_message=u"<h1>Ueberschrift</h1>")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 98, in mail_admins
    mail.send(fail_silently=fail_silently)
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 255, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 95, in send_messages
    sent = self._send(message)
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 113, in _send
    force_bytes(message.as_string(), charset))
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 169, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.6/email/generator.py", line 84, in flatten
    self._write(msg)
  File "/usr/lib/python2.6/email/generator.py", line 109, in _write
    self._dispatch(msg)
  File "/usr/lib/python2.6/email/generator.py", line 135, in _dispatch
    meth(msg)
  File "/usr/lib/python2.6/email/generator.py", line 201, in _handle_multipart
    g.flatten(part, unixfrom=False)
  File "/usr/lib/python2.6/email/generator.py", line 84, in flatten
    self._write(msg)
  File "/usr/lib/python2.6/email/generator.py", line 109, in _write
    self._dispatch(msg)
  File "/usr/lib/python2.6/email/generator.py", line 135, in _dispatch
    meth(msg)
  File "/usr/lib/python2.6/email/generator.py", line 178, in _handle_text
    self._fp.write(payload)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 1: ordinal not in range(128)

# Umlaut-character in the html message --> error.
>>> mail_admins(u"Test 4", u"Message Body", html_message=u"<h1>Überschrift</h1>")
Traceback (most recent call last):
    [ Exact same stack trace as above. ]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xdc' in position 4: ordinal not in range(128)

# As before, Umlaut-character in the html message, but plain "" byte string, not u"" --> ok.
>>> mail_admins(u"Test 5", u"Message Body", html_message="<h1>Überschrift</h1>")

Change History (1)

comment:1 by Claude Paroz, 11 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #20230. Consider upgrading to a more recent Python version, I think this is fixed in Python 2.6.6.

Note: See TracTickets for help on using tickets.
Back to Top