googleappengine:从表单数据发送电子邮件?

2024-04-26 00:11:09 发布

您现在位置:Python中文网/ 问答频道 /正文

如何为某些表单数据生成响应和电子邮件。我试过这样的方法:

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 -


Tags: namepyimportselfapiappmessageget
2条回答

发现了:糟糕的文档! 这样做有效:

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)

应用程序必须匹配,因此如果您愿意,可以将其扩展到应用程序,或者应用程序xxxpy不起作用!你知道吗

你试过浏览localhost:8080/mail-me.py吗?这就是路由用来调用get()方法的方法。你知道吗

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

相关问题 更多 >