更新: 我已经发现,当Gmail自动转发收到的电子邮件时,它会保留电子邮件原始标题,这不会被我的代码显示但会引发错误。 this我设法得到了标题。 如何避免因为非Ascii电子邮件标题引发异常?。我也可以忽略它的解决方案!
原始邮寄: 我有这个恼人的问题 UnicodeDecodeError:'ascii'编解码器无法解码位置的字节0xc3 。我已经尝试了很多东西,因为很多人问过类似的问题,但这些解决方案都没有。该代码适用于非ASCII。
import logging
import webapp2
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.api import mail
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
tobesent = mail_message.subject
logging.info(mail_message.subject)
logging.info(mail_message.date)
tobesent = ''.join(c for c in tobesent if c.isdigit())
mail.send_mail(
sender="senderaddress@gmail.com",
to="receiveraddress@gmail.com",
subject="hello bello",
body= "fontos informacio: " + str(tobesent)
)
app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)
我在GAE日志中遇到的错误如下:
'ascii' codec can't decode byte 0xc3 in position 18: ordinal not in range(128)
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 69, in post
self.receive(mail.InboundEmailMessage(self.request.body))
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 743, in __init__
self.update_from_mime_message(mime_message)
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 1305, in update_from_mime_message
super(InboundEmailMessage, self).update_from_mime_message(mime_message)
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 1214, in update_from_mime_message
super(EmailMessage, self).update_from_mime_message(mime_message)
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 1094, in update_from_mime_message
subject = _decode_and_join_header(mime_message['subject'], separator=u'')
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 559, in _decode_and_join_header
for s, c in email.header.decode_header(header))
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 559, in <genexpr>
for s, c in email.header.decode_header(header))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 18: ordinal not in range(128)
我已尝试在互联网上找到的所有解决方案,例如.encode('utf8')
或decode.encode('utf8')
或unicoded = unicode(non_unicode_string, source_encoding)
其中source_encoding类似于'cp1252','iso-8859-1'等,并将其发送到输出(由Daniel Beck提供),所以我希望这不是重复但是那么解决方案是什么?什么都行不通。
以下是发生的情况:我收到了一封自动发送到我的Gmail帐户的电子邮件,该帐户会自动将其转发(在过滤器设置下)到我的me@myapp.appspotmail.com。该电子邮件中还包含éíőűöüó字母,也包含在主体内。那就是问题所在。这将使整个事情失败。 我在Gmail中尝试了外发电子邮件设置对外发邮件使用Unicode(UTF-8)编码。只有当我手动转发电子邮件时,这才有效。
app.yaml in case:
application: myappid
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /_ah/mail/myaddress@myappid.appsportmail.com
script: myappid.app
login: admin
- url: /.*
script: myappid.app
inbound_services:
- mail
libraries:
- name: webapp2
version: "2.5.2"