Google App Engine python入站邮件LookupError:未知编码

时间:2017-08-31 19:05:52

标签: python google-app-engine google-cloud-platform

我按照示例in the docs使用“标准”入站邮件处理程序向我的Google App Engine应用收到入站电子邮件。

似乎发送给应用程序(而不是我)的某个电子邮件消息导致电子邮件api抛出LookupError:未知编码异常。请看回溯。

据我所知,在调用应用程序的邮件处理程序之前抛出此异常,显然无法通过应用程序的代码捕获和处理。这是真的吗?

似乎App Engine以大约40分钟的间隔重试失败的消息,这会继续产生错误(和警报......)有没有办法中止这个?

感谢您的帮助。

回溯:

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 70, in post
    self.receive(mail.InboundEmailMessage(self.request.body))
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 939, in __init__
    self.update_from_mime_message(mime_message)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1513, in update_from_mime_message
    super(InboundEmailMessage, self).update_from_mime_message(mime_message)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1422, in update_from_mime_message
    super(EmailMessage, self).update_from_mime_message(mime_message)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1291, in update_from_mime_message
    subject = _decode_and_join_header(mime_message['subject'], separator=u'')
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 597, in _decode_and_join_header
    for s, charset in email.header.decode_header(header))
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 597, in 
    for s, charset in email.header.decode_header(header))
LookupError: unknown encoding: iso-8859-8-i

1 个答案:

答案 0 :(得分:4)

调用入站邮件处理程序的post方法时会发生错误。

  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 70, in post
    self.receive(mail.InboundEmailMessage(self.request.body))

最简单的解决方案是覆盖您自己的处理程序中的post方法以捕获错误:

import logging
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler


class MyInboundMailHandler(InboundMailHandler):

    def post(self):
        try:
            super(MyInboundMailHandler, self).post()
        except LookupError as ex:
            logging.warning('Could not process message because %s.', ex)

    def receive(self, mail_message):
        # Process message

如果您不想丢失消息,可以创建并注册自定义iso-8859-8-i编解码器。这似乎不是一个记录良好的过程,但这些问题提供了一些提示:

How do I properly create custom text codecs?

Custom Python Charmap Codec

how do I write a custom encoding in python to clean up my data?

标准库iso-8859-8 encoding提供了一个很好的模板。