0

我在 Google Apps Engine、Python 上托管我的网站,我正在尝试处理一个简单的联系表单。

这是我的 HTML:

<form method="post" action="/email" id="contactForm">  
    <h2>Let's get in touch!</h2>
    Name:<br/>
    <input size=35 name="name" placeholder="Feature coming soon!"><br/>
    Email:<br/>
    <input size=35 name="email" placeholder="Feature coming soon!"><br/>
    Subject:<br/>
    <input size=35 name="subject" placeholder="Feature coming soon!"><br/>
    Message:<br/>
    <textarea name="message" rows=15 cols=50 placeholder="Feature coming soon!"></textarea><br/>
    <input type="submit" name="send" value="Submit">
</form>

在我的 app.yaml 中:

  • 网址:/电子邮件
    脚本:email.py

这是我的 email.py:

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

class SendEmail(webapp.RequestHandler):
    def post(self):
        name = self.request.post("name")
        email = self.request.post("email")
        tempSubject = self.request.post("subject")
        msg = self.request.post("body")

        if name is None:
            self.response.out.write("Error: You did not enter a name.")
        elif email is None:
            self.response.out.write("Error: You did not enter an email.")
        elif tempSubject is None:
            self.response.out.write("Error: You did not enter a subject.")
        elif msg is None:
            self.response.out.write("Error: You did not enter a message.")
        else:
            _subject = "Msg from: " + name + "Re: " + tempSubject

            message = mail.EmailMessage(sender = "alexyoung1992@alexyoung.us", to = "alexyoung1992@gmail.com", subject = _subject, body = msg, reply_to = email)
            message.send()

application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
run_wsgi_app(application)
self.redirect('/')

我收到 500 服务器错误:

错误:服务器错误

服务器遇到错误,无法完成您的请求。如果问题仍然存在,请报告您的问题并提及此错误消息和导致它的查询。

编辑:我更新了我的 Python 脚本并在我的本地机器上调试了所有它。一切运行顺利,但是一旦我尝试在服务器上运行它,我就会遇到同样的错误。你们也可以尝试自己运行脚本,看看是否有任何不同。

4

2 回答 2

2

Python 使用“和”而不是 && Name != None && Email != None

在本地环境中调试会容易得多。并显示错误的引用。

于 2013-02-07T06:22:54.383 回答
0

事实证明,服务器没有识别我的一些变量,因为我在某些地方使用空格缩进,在其他地方使用制表符。这是最终代码:

import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

class SendEmail(webapp.RequestHandler):
    def post(self):
        name = self.request.get('name', '')
        email = self.request.get('email', '')
        tempSubject = self.request.get('subject', '')
        msg = self.request.get('message', '')

        if name is None:
            self.response.out.write("Error: You did not enter a name.")
        elif email is None:
            self.response.out.write("Error: You did not enter an email.")
        elif tempSubject is None:
            self.response.out.write("Error: You did not enter a subject.")
        elif msg is None:
            self.response.out.write("Error: You did not enter a message.")
        else:
            _subject = "Message from: " + name + ", Re: " + tempSubject
            msg += "\n\nI can be reached at "
            msg += email

            message = mail.EmailMessage(sender = "foo@bar.com", to = "bar@foo.com")
            message.subject = _subject
            message.body = msg
            message.send()

            self.redirect('/')

def runApp():
    application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    runApp()
于 2013-02-10T23:11:03.900 回答