简体   繁体   English

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

[英]Google App Engine python inbound mail LookupError: unknown encoding

I am receiving inbound email to my Google App Engine app, using a "standard" inbound mail handler, following the examples in the docs . 按照docs中的示例我正在使用“标准”入站邮件处理程序接收到我的Google App Engine应用程序的入站电子邮件。

It seems that a certain email message sent to the app (not by me) is causing the email api to throw a LookupError: unknown encoding exception. 似乎发送给应用程序的某些电子邮件消息(不是我本人造成的)导致电子邮件api抛出LookupError:未知编码异常。 Please see the traceback. 请参阅回溯。

As far as I can tell this exception is thrown before the app's mail handler is invoked, apparently making it impossible to catch and handle by the app's code. 据我所知,此异常是在调用应用程序的邮件处理程序之前引发的,显然使它无法被应用程序的代码捕获和处理。 Is this really the case ? 真的是这样吗?

It also seems that App Engine retries the failed message at approximately 40 minutes intervals, which continues to produce errors (and alerts...) Is there any way to abort this ? 同样,App Engine似乎每隔40分钟重试一次失败的消息,这会继续产生错误(和警报...),是否有任何方法可以中止此操作?

Thanks for your help. 谢谢你的帮助。

Traceback: 追溯:

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

The error is happening when the inbound mail handler's post method is called. 调用入站邮件处理程序的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))

The simplest solution is to override the post method in your own handler to trap the error: 最简单的解决方案是在您自己的处理程序中覆盖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

If you don't want to lose the message, you could create and register a custom iso-8859-8-i codec. 如果您不想丢失该消息,则可以创建并注册自定义的iso-8859-8-i编解码器。 This doesn't seem to be a well-documented process, but these questions provide some hints: 这似乎不是一个有据可查的过程,但是这些问题提供了一些提示:

How do I properly create custom text codecs? 如何正确创建自定义文本编解码器?

Custom Python Charmap Codec 自定义Python Charmap编解码器

how do I write a custom encoding in python to clean up my data? 如何在python中编写自定义编码以清理数据?

And the standard library's iso-8859-8 encoding provides a good template. 标准库的iso-8859-8编码提供了一个很好的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM