- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,当我尝试在 Django 中使用 EmailMessage 发送电子邮件时出现此错误。
UnicodeEncodeError 在/checkout/
'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)
最佳答案
所以在尝试和尝试之后,我找到了适合我的解决方案。
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()
我希望这对遇到同样问题的人有所帮助。
关于python - EmailMessage Django 错误上带有 attach_file 的 UnicodeEncodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885062/
尽管文件存在,但 attach_file 没有选择绝对 url。它能够获取内部 url 并发送文件,但不能获取绝对 url email.attach_file("http://devuserapi.d
我在尝试使用 Capybara 的 attach_file 方法测试图像上传是否适用于回形针时遇到问题。 rspec 在下面返回这些错误: 失败: 1) Upload Background Image
因此,当我尝试在 Django 中使用 EmailMessage 发送电子邮件时出现此错误。 UnicodeEncodeError 在/checkout/ 'ascii' codec can't en
我是一名优秀的程序员,十分优秀!