gpt4 book ai didi

python - EmailMessage Django 错误上带有 attach_file 的 UnicodeEncodeError

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:26 24 4
gpt4 key购买 nike

因此,当我尝试在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com