google-app-engine:从表单数据发送电子邮件?

google-app-engine: Sending email from form data?

如何为某些表单数据生成响应和电子邮件。我试过类似的东西:

import webapp2
from google.appengine.api import mail

MAIN_PAGE_HTML = """\
<html>
  <body>
   <h1>response to GET</h1>
  </body>
</html>
"""

class My_Email_Class(webapp2.RequestHandler):
      def get(self):
          user_address = self.request.get("email")
          user_name = self.request.get("name")

          if not mail.is_email_valid(user_address):
              pass

          message = mail.EmailMessage()
          message.sender = 'xyz@xyz.com'
          message.to = 'abcd@abcd.com'
          message.subject = "Website:" + str(user_name + user_email)
          message.body = """\
Hi
"""
          message.send()

          self.response.write(MAIN_PAGE_HTML)

app = webapp2.WSGIApplication([('/mail-me.py', My_Email_Class),], debug=True)

我正在通过 app.yaml 和脚本行映射脚本:

- url: /mail-me.py.*
  script: mail-me.py

我正在调用它:dev_appserver.py ~root/whatever/ 和浏览:localhost:8080

我没有看到任何错误,但浏览器中也没有响应页面


也尝试过(app.yaml):

application: hello
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: main.py

main.py

import os
import webapp2
class MainHandler(webapp2.RequestHandler):
  def get(self):
    print 'Content-Type: text/plain'
    print ''
    print 'Hello, world!'

application = webapp2.WSGIApplication([(r'/', MainHandler)], debug=True)

INFO 2015-11-10 06:29:51,805 module.py:794] default: "GET / HTTP/1.1" 200 -

您是否尝试浏览至 localhost:8080/mail-me.py?这就是您的路由用来调用您的 get() 方法的内容。

您不需要在 URL 中包含 .py。请改用 ('/mail-me', My_Email_Class),然后浏览至 localhost:8080/mail-me.

想通了:糟糕的文档! 这有效:

import os
import webapp2

class MainHandler(webapp2.RequestHandler):
  def get(self):
    print 'Content-Type: text/plain'
    print ''
    print 'Hello, world!'

app = webapp2.WSGIApplication([(r'/', MainHandler),], debug=True)


application: hello
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: main.app

script: main.app

app = webapp2.WSGIApplication([(r'/', MainHandler),], debug=True)

app 必须匹配,因此您可以根据需要将其扩展为 applicationappxxx. py 虽然不起作用!