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)