1

我在 Google Apps Engine 上托管一个网站,并尝试使用 Python 的邮件 API 来获取 POST 数据并发送电子邮件。

这是我的脚本:

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')
        # self.response.out.write(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 = "Msg from: " + name + "Re: " + tempSubject

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

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

if __name__ == '__main__':
    runApp()

这是服务器上日志的回溯:

<type 'exceptions.NameError'>: name 'name' is not defined
Traceback (most recent call last):
  File "/base/data/home/apps/s~alex-young/1.365202894602706277/email.py", line 5, in <module>
    class SendEmail(webapp.RequestHandler):
  File "/base/data/home/apps/s~alex-young/1.365202894602706277/email.py", line 14, in SendEmail
    if name is None:

我在本地运行脚本没有任何错误,但是一旦我尝试在服务器上运行它,它就会一直坚持name我声明的变量不存在。知道为什么会这样吗?

另外,如果我注释掉那一行,它email会说不存在,依此类推

4

1 回答 1

1

事实证明,有时我使用空格来缩进,而其他时候我使用制表符。Python 不喜欢这样。这是最终代码:

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:15:32.617 回答