views:

58

answers:

1

I'm writing an app for Google App Engine (with Python and Django) that needs to receive email and add some elements of the received email messages to a datastore. I am a very novice programmer.

The problem is that the script I specify to handle incoming email appears to only run once (until the script is touched).

Sending a test email from the local admin console to, say, '[email protected]' causes an entity to be added to the local datastore correctly.

Sending a second, third, etc. test email has no effect - the entity is not added.

'Touching' handle_incoming_email.py (which I understand to mean adding or deleting a space and then saving), then sending another test email, will cause the entity to be added correctly.

app.yaml:

application: downloadtogo
version: 1
runtime: python
api_version: 1

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.py

- url: /_ah/mail/.+
  script: handle_incoming_emaril.py
  login: admin

inbound_services:
- mail

handle_incoming_email.py:


from downloadtogo.models import Email

import logging, email
import wsgiref.handlers
import exceptions

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

class MailHandler(InboundMailHandler):
  def receive(self, message):
    email = Email()
    email.from_address = message.sender
    email.put()

def main(): 
    application = webapp.WSGIApplication([MailHandler.mapping()], debug=True)
    wsgiref.handlers.CGIHandler().run(application)

main()

models.py:


from appengine_django.models import BaseModel
from google.appengine.ext import db

class Email(db.Model):
  from_address = db.StringProperty()
  to_address = db.StringProperty()
  body = db.StringProperty(multiline=True)
  added_on = db.DateTimeProperty(auto_now_add=True)
+4  A: 

Handlers are matched in order. .* matches any request, so the email handler will never match at all. Put .* last.

Wooble
Thanks! I didn't realize that.Somehow, handle_incoming_email.py was being called elsewhere - not only was it not being matched (as per your answer) but I mistyped it in app.yaml (as per zovision's comment above).Now that it's being matched, sending a test email causes "KeyError: 'warnings'" - I'll try to catch this.
bfox
Among other issues, I had Python 2.7 on my computer as well as the required Python 2.5 - this is what was causing the KeyError.Thanks again for your help!
bfox