简体   繁体   中英

UnicodeEncodeError with attach_file on EmailMessage Django error

So I get this error when I try to send an email with EmailMessage in Django.

UnicodeEncodeError at /checkout/

'ascii' codec can't encode character u'\u0161' in position 15:

The body of the message contains some unicode character that breaks the script.

The thing is that everything works just fine if I omit the attachment OR if the body doesn't contain any unicode characters. The subject can contain unicode characters with no unicode error. So it happens only in combination with unicode characters in the body and the attached file. This seems like a bug to me.

The attachemnt is a generated pdf file.

The code running on Ubuntu 10.04, apache2, mod_wsgi, python 2.6.5, Django 1.5

The code I am using is

t = loader.get_template('orders/invoice_email.html')
c = {
    'order': order,
}

email = EmailMessage(subject, t.render(Context(c)), from_mail, [to, ])
email.encoding = "utf-8"
email.content_subtype = "html"
email.attach_file(invoice.name)
email.send()

and the traceback

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in     get_response
115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
25.                 return view_func(request, *args, **kwargs)
File "/var/www/projects/vitamei-shop/modules/orders/views.py" in checkout
48.             order = form.save()            
File "/var/www/projects/vitamei-shop/modules/orders/forms.py" in save
71.         email_invoice(order)
File "/var/www/projects/vitamei-shop/vitamei/.././modules/orders/views.py" in email_invoice
320.     email.send()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py" in send
255.         return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py" in send_messages
95.                 sent = self._send(message)
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py" in _send
113.                     force_bytes(message.as_string(), charset))
File "/usr/local/lib/python2.6/dist-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 at /checkout/
Exception Value: 'ascii' codec can't encode character u'\u0161' in position 15: ordinal not in range(128)

So after trying and trying I found a solution that worked for me.

t = loader.get_template('orders/invoice_email.html')
c = {
        'order': order,
    }

body = u''.join(t.render(Context(c))).encode('utf-8').strip()
email = EmailMessage(subject, body, from_mail, [to, ])
email.encoding = "utf-8"
email.content_subtype = "html"
email.attach_file(invoice.name, mimetype="application/pdf")
email.send()

I hope this will help if someone has the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM