python - Django 1.5 UnicodeEncodeError using attach_alternative in emails -
i'm in process of migrating django project 1.3 1.5.
one app sends emails not working in 1.5 in development machine. error:
unicodeencodeerror @ /envia_activacion/ 'ascii' codec can't encode character u'\xf3' in position 37: ordinal not in range(128)
traceback: file "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 115. response = callback(request, *callback_args, **callback_kwargs) file "/home/django/clones/migracion/libs/my_apps/emailing/views.py" in envia_email 48. mailer.msg.send() file "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/message.py" in send 255. return self.get_connection(fail_silently).send_messages([self]) file "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in send_messages 95. sent = self._send(message) file "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in _send 113. force_bytes(message.as_string(), charset)) file "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/message.py" in as_string 169. g.flatten(self, unixfrom=unixfrom) file "/usr/lib/python2.6/email/generator.py" in flatten 84. self._write(msg) file "/usr/lib/python2.6/email/generator.py" in _write 109. self._dispatch(msg) file "/usr/lib/python2.6/email/generator.py" in _dispatch 135. meth(msg) file "/usr/lib/python2.6/email/generator.py" in _handle_multipart 201. g.flatten(part, unixfrom=false) file "/usr/lib/python2.6/email/generator.py" in flatten 84. self._write(msg) file "/usr/lib/python2.6/email/generator.py" in _write 109. self._dispatch(msg) file "/usr/lib/python2.6/email/generator.py" in _dispatch 135. meth(msg) file "/usr/lib/python2.6/email/generator.py" in _handle_text 178. self._fp.write(payload) exception type: unicodeencodeerror @ /envia_activacion/ exception value: 'ascii' codec can't encode character u'\xf3' in position 37: ordinal not in range(128)
my code this:
plaintext = get_template('emailing/activacion.txt') htmly = get_template('emailing/activacion.html') //html template utf-8 charset d = context({ 'code': code, 'version': sversion, 'activacion': activacion, 'distribuidor': distribuidor, 'contacto': contacto, 'departamento': departamento, 'empresa': empresa }) asunto = u'código activación %s - v%s - code: %s' % (nombre_destinatario, version, code) text_content = plaintext.render(d) html_content = htmly.render(d) msg = emailmultialternatives(asunto, text_content, remitente, para, [remitente], headers={'reply-to': remitente}) msg.attach_alternative(html_content, "text/html")//this makes fail msg.send()
this works in django 1.3, don't know in django 1.4.
debugging, see text fails unicode:
> /usr/lib/python2.6/email/generator.py(178)_handle_text() -> self._fp.write(payload) (pdb) s unicodeencodeerror: unicodee...ge(128)') > /usr/lib/python2.6/email/generator.py(178)_handle_text() -> self._fp.write(payload) (pdb) payload u'apreciado cliente, le indicamos los c\xf3digos necesarios ...'
i'm using python 2.6.5. i've reviewed ticket looks same https://code.djangoproject.com/ticket/19107 in django 1.5 patch exists , i'm still getting error.
related: unicode error in python
i appreciate help. thanks
your payload unicode
instance (note django strives use unicode
internally) while should str
instance. it's responsability pass (preferably encoded) str
. quite easy fwiw:
# assume use utf-8 everywhere text_content = plaintext.render(d).encode("utf-8") html_content = htmly.render(d).encode("utf-8")
Python - Django 1.5 Unicodeencodeerror Using Attach_Alternative In
ReplyDeleteEmails - >>>>> Download Now
>>>>> Download Full
Python - Django 1.5 Unicodeencodeerror Using Attach_Alternative In
Emails - >>>>> Download LINK
>>>>> Download Now
Python - Django 1.5 Unicodeencodeerror Using Attach_Alternative In
Emails - >>>>> Download Full
>>>>> Download LINK E3