2013-10-04 23 views
1

在我的生產日誌我看到許多錯誤,如下面InboundEmailMessage引發的UnicodeDecodeError例外

0.1.0.20 - - [04/Oct/2013:11:07:03 -0700] "POST /_ah/mail/[email protected] HTTP/1.1" 500 0 - - "myapp.appspot.com" ms=258 cpu_ms=27 app_engine_release=1.8.5 instance=00c61b117c920d68f9913bdef33c5b25f4288840 

E 2013-10-04 22:07:03.434 
    'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128) 
    Traceback (most recent call last): 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__ 
     rv = self.handle_exception(request, response, e) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__ 
     rv = self.router.dispatch(request, response) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher 
     return route.handler_adapter(request, response) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__ 
     return handler.dispatch() 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch 
     return self.handle_exception(e, self.app.debug) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch 
     return method(*args, **kwargs) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 69, 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 775, 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 1337, 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 1246, 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 1126, 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 591, in _decode_and_join_header 
     for s, c in email.header.decode_header(header)) 
     File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 591, in <genexpr> 
     for s, c in email.header.decode_header(header)) 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128) 

我怎樣才能得到更多的細節會發生什麼?我怎樣才能妥善處理這個異常?

回答

1

我沒有unicode的問題與此代碼(不InboundMailHandler):

message = mail.InboundEmailMessage(request.body) 
msg_body = message.subject 

編輯:

我在dev_appserver > Inbound Mail做了一些測試,在主題欄"àèé"和對象是Unicode,但如果您的msg_body不是unicode,請首先找到type(msg_body)的編解碼器並首先解碼該主題。

message = mail.InboundEmailMessage(request.body) 
msg_body = message.subject # if unicode or 
msg_body = message.subject.decode("ascii") # if ascii then 
_str = msg_body.encode("utf-8") # str 
+0

感謝。我遇到的主要問題是這個問題只發生在一些電子郵件上,而不是所有電子郵件。甚至在我開始解析它之前。我甚至不知道如何獲得消息的細節,這導致了這個問題。 –

+0

拋開'InboundMailHandler'可以幫助調試。只有找到「奇怪」的字符時,你的應用纔會提出unicode錯誤。如果您提交的是主題。順便說一句,編碼py27是一個黑洞。 –

+0

我是否正確理解你建議使用'webapp2.RequestHandler' /'post'而不是'InboundMailHandler'? –

0
import base64 

message_raw = request.body.encode('UTF-8') 
message_decoded = base64.urlsafe_b64decode(message_raw) 
message = InboundEmailMessage(message_decoded)