Google App Engine与Django - InboundMailHandler似乎只工作一次
我正在为Google App Engine(使用Python和Django)写一个应用程序,这个应用需要接收电子邮件,并将收到的邮件中的一些内容添加到数据存储中。我还是个编程新手。
问题是,我指定用来处理来信的脚本似乎只运行一次(直到我对脚本进行修改)。
从本地管理控制台发送一封测试邮件到,比如说'test@downloadtogo.appspotmail.com',可以正确地将一个实体添加到本地数据存储中。
但是,发送第二封、第三封等测试邮件就没有效果了——实体不会被添加。
如果我对handle_incoming_email.py
进行“修改”(我理解为添加或删除一个空格,然后保存),然后再发送另一封测试邮件,就会正确地添加实体。
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)
1 个回答
5
处理程序是按顺序匹配的。.*
可以匹配任何请求,所以邮件处理程序根本不会被匹配到。把 .*
放在最后。