#19107 closed Bug (fixed)

Can't send unicode e-mail

Reported by:… Owned by: nobody
Component: Core (Mail) Version: master
Severity: Normal Keywords: mail unicode
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Hello guys,

I'm on django dev and having troubles sending unicode emails. Here's the relevant code:

# person is a normal django user
subject = u"Welcome to Odeio - %s" %
message = render_to_string('accounts/registration_email.txt', ctx_dict)
person.email_user(subject, message)


'ascii' codec can't encode character u'\xe1' in position 2: ordinal not in range(128)


File "/odeio/django/core/mail/", line 62, in send_mail
  File "/odeio/django/core/mail/", line 251, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/odeio/django/core/mail/backends/", line 91, in send_messages
    sent = self._send(message)
  File "/odeio/django/core/mail/backends/", line 106, in _send
    self.connection.sendmail(from_email, recipients, email_message.message().as_string())
  File "/odeio/django/core/mail/", line 141, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/opt/bitnami/python/lib/python2.6/email/", line 84, in flatten
  File "/opt/bitnami/python/lib/python2.6/email/", line 109, in _write
  File "/opt/bitnami/python/lib/python2.6/email/", line 135, in _dispatch
  File "/opt/bitnami/python/lib/python2.6/email/", line 178, in _handle_text
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 2: ordinal not in range(128)

Thanks in advance for the help!

Attachments (1)

19107.diff (777 bytes) - added by claudep 21 months ago.
Workaround for python bug 1368247

Download all attachments as: .zip

Change History (11)

comment:1 Changed 21 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Can you isolate if the encoding error is due to unicode in subject, in message, or both? If it is related to message, can you also give us the output of the render_to_string call?

comment:2 Changed 21 months ago by…

Hello claudep,
thank you for your reply. I believe the encoding error is due to unicode in the message, because the character at position 2 in the message is "á", which is not ASCII. It seems like the subject is ok, even though it might contain unicode characters ( might be unicode). Here's the output of render_to_string:

Olá My name,

seja bem-vindo ao sistema de administração jurídica online Odeio. O escritório Departamento Jurídico cadastrou-lhe no sistema. Os seus dados são:

Nome: My name
Usuário / Email:

Para validar o seu cadastro e criar uma senha, entre no seguinte link:

Qualquer dúvida, sugestão ou crítica não hesite em entrar em contato. Seu feedback é muito bem-vindo.

Equipe Odeio

Thanks again for your help.

comment:3 Changed 21 months ago by…

I have just double checked the subject and message. Removing all unicode characters from the message results in the message being sent, even when I added unicode characters to the subject.

comment:4 Changed 21 months ago by claudep

Thanks, can you still tell us the exact version of Python, the encoding of the file, the value of settings.FILE_CHARSET, settings.DEFAULT_CHARSET and sys.getdefaultencoding()?

comment:5 Changed 21 months ago by…

Hi claudep,

thanks for the quick reply. My python version is Python 2.6.5. I haven't set FILE_CHARSET neither DEFAULT_CHARSET, so I assume it's utf-8. I'm going to set it manually and try again. sys.getdefaultencoding() returns 'ascii'. For the file I did this: file -i registration_email.txt
registration_email.txt: text/plain; charset=utf-8. So I guess it should be okay.

I've tried again setting FILE_CHARSET and DEFAULT_CHARSET to utf-8 but it still fails with the same stacktrace.

If you want I can change the code to introduce any tests you wish. Many thanks again for the help.

comment:6 Changed 21 months ago by claudep

I think i get it. This issue is probably related to that has been fixed in Python 2.6.6 ( I will attach a patch with a workaround for Python < 2.6.6. Can you test it?

Changed 21 months ago by claudep

Workaround for python bug 1368247

comment:7 Changed 21 months ago by…

OH. MY. GOD. Seriously, are you a wizard? Because that's some serious black magic you just performed. That did fix it. How did you manage to find such bug in python? Wow... many many many many many thanks. That'd have taken me 10 years to figure out.

comment:8 Changed 21 months ago by claudep

Great :-) It is not so magic. I couldn't reproduce the failure. The only obvious difference between your setup and mine was the Python version. So I looked for the NEWS file of recent Python 2.6 releases and searched for issues related to MIME. I admit I was lucky to find a match so quickly.

comment:9 Changed 21 months ago by claudep

  • Has patch set
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:10 Changed 21 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 501d793398c3ecc3c8a54475d07609cf34643553:

Fixed #19107 -- Workarounded message-encoding bug on Python < 2.6.6

Thanks Bernardo Pires for the report.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.