1

更新:我发现当 Gmail 自动转发传入的电子邮件时,它会保留我的代码不显示但会引发错误的电子邮件原始标题。有了这个,我设法得到了标题。我怎样才能避免它因为非 Ascii 电子邮件标题而引发异常?. 也欢迎我可以忽略它的解决方案!

原始帖子:我遇到了UnicodeDecodeError 这个烦人的问题:'ascii' codec can't decode byte 0xc3 in position ...。自从许多人提出类似的问题以来,我已经尝试了很多事情,但没有一个解决方案在这里有效。该代码适用于非 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')or 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 以防万一:

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"
4

0 回答 0