3

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

/checkout/ 处的 UnicodeEncodeError

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

消息的正文包含一些破坏脚本的 Unicode 字符。

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

附件是生成的 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)
4

1 回答 1

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

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

于 2013-04-10T08:19:24.470 回答