Thursday, May 2, 2013

Django 1.5.1: Sending HTML mails with Unicode string contents fails

Hi all,

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>")


I'm not sure if the problem is with my code or in Django.
Can someone help please?

Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment