简体   繁体   中英

Python GAE: incoming mail handler error

I'm writing application on GAE that can parse and store incoming mails. I've prepared some simple code for email parsing, but something goes wrong, when I try to simulate e-mail recieveing from admin dev console on local dev server:

/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)

What I'm doing wrong?

i think what happens is that in your app.yaml you define the module/file as the script instead of an application, the module is not callable of course.

change the app.yaml definition to:

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

and add this line at the end of email_handler.py

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

here the docs: https://developers.google.com/appengine/docs/python/mail/receivingmail

The problem is you don't have declare a WSGIApplication for your handler LogSenderHandler .

You have to read about that on: 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)

After that you have to specify the WSGI application in your app.yaml

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

Note: As is written in the documentation, the class InboundMailHandler has a special method mapping helps you to declare the map of URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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