与EmailMessage上的attach_file的UnicodeEncodeError Django错误

时间:2013-04-08 17:14:40

标签: python django

当我尝试在Django中发送带有EmailMessage的电子邮件时,我收到此错误。

/ checkout /

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

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

问题是,如果我省略附件或者正文不包含任何unicode字符,一切正常。主题可以包含unicode字符,没有unicode错误。因此,它仅与正文中的unicode字符和附加文件组合使用。这对我来说似乎是个错误。

attachemnt是生成的pdf文件。

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

1 个答案:

答案 0 :(得分:4)

因此,经过尝试和尝试,我找到了一个适合我的解决方案。

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

如果有人遇到同样的问题,我希望这会有所帮助。