Python GAE:传入邮件处理程序错误

时间:2012-05-10 15:46:43

标签: python google-app-engine email handler

我在GAE上编写可以解析和存储传入邮件的应用程序。我准备了一些简单的电子邮件解析代码,但当我尝试模拟从本地开发服务器上的admin dev控制台收到的电子邮件时出现问题:

/develop/google_appengine/google/appengine/runtime/wsgi.py", line 193, in Handle
    result = handler(self._environ, self._StartResponse)
TypeError: 'module' object is not callable
INFO     2012-05-08 16:14:43,516 dev_appserver.py:2891] "POST /_ah/mail/test@example.com HTTP/1.1" 500 -

的app.yaml:

application: mailhandler
version: 1
runtime: python27
api_version: 1
threadsafe: true

inbound_services:
- mail

handlers:
- url: /_ah/mail/.+ 
  script: email_handler
  login: admin

email_handler.py:

from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

from models import DataStoreManager

class LogSenderHandler(InboundMailHandler):
    # Receiving new mail message and parsing it
    def receive(self, mail_message):                                                                                                                      
        manager = DataStoreManager()
        instance = manager.get_instance_by_email(mail_message.sender.lowercase())

        email_key = manager.store_email(instance, instance.user, mail_message, mail_message.attachments)

我做错了什么?

2 个答案:

答案 0 :(得分:7)

我认为发生的事情是,在你的app.yaml中,你将模块/文件定义为脚本而不是应用程序,模块当然不可调用。

app.yaml定义更改为:

handlers:
- url: /_ah/mail/.+ 
  script: email_handler.application
  login: admin

并在email_handler.py

的末尾添加此行
application = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)

这里的文档: https://developers.google.com/appengine/docs/python/mail/receivingmail

答案 1 :(得分:3)

问题是您没有为处理程序LogSenderHandler声明WSGIApplication。

您必须阅读以下内容: https://developers.google.com/appengine/docs/python/python27/using27

from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

from models import DataStoreManager

class LogSenderHandler(InboundMailHandler):
    # Receiving new mail message and parsing it
    def receive(self, mail_message):                                                                                                                      
        manager = DataStoreManager()
        instance = manager.get_instance_by_email(mail_message.sender.lowercase())

        email_key = manager.store_email(instance, instance.user, mail_message, mail_message.attachments)

application = webapp.WSGIApplication([LogSenderHandler.mapping()], debug=True)

之后,您必须在your app.yaml

中指定WSGI应用程序
handlers:
- url: /_ah/mail/.+ 
  script: email_handler.application
  login: admin

注意:正如文档中所写,类InboundMailHandler有一个特殊方法mapping可以帮助您声明URL地图。