我正在为 Google App Engine(使用 Python 和 Django)编写一个应用程序,它需要接收电子邮件并将接收到的电子邮件消息的一些元素添加到数据存储区。我是一个非常新手的程序员。
问题是我指定用于处理传入电子邮件的脚本似乎只运行一次(直到脚本被触及)。
从本地管理控制台向“test@downloadtogo.appspotmail.com”发送测试电子邮件会导致实体被正确添加到本地数据存储。
发送第二个、第三个等测试电子邮件无效 - 未添加实体。
“触摸”handle_incoming_email.py
(我的理解是添加或删除空格然后保存),然后发送另一封测试电子邮件,将导致实体正确添加。
应用程序.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()
模型.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)
最佳答案
处理程序按顺序匹配。 .*
匹配任何请求,因此电子邮件处理程序永远不会匹配。将 .*
放在最后。
关于python - Google App Engine with/Django - InboundeMailHandler 似乎只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593902/