UnicodeEncodeError与EmailMessage Django上的attach_file错误 [英] UnicodeEncodeError with attach_file on EmailMessage Django error

查看:22
本文介绍了UnicodeEncodeError与EmailMessage Django上的attach_file错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我尝试在Django中使用EmailMessage发送电子邮件时,会出现此错误.

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

/checkout/

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

邮件正文包含一些会破坏脚本的unicode字符.

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

问题是,如果我省略附件,或者如果主体不包含任何unicode字符,一切都将正常工作.主题可以包含没有unicode错误的unicode字符.因此,仅在正文和附件中与Unicode字符结合使用时才发生.对我来说这似乎是个虫子.

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.

附件是生成的pdf文件.

The attachemnt is a generated pdf file.

在Ubuntu 10.04,apache2,mod_wsgi,python 2.6.5,Django 1.5上运行的代码

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

我正在使用的代码是

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

和追溯

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.

这篇关于UnicodeEncodeError与EmailMessage Django上的attach_file错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆