【问题标题】:GAE + Python + Sendmail + Form = mail not workingGAE + Python + Sendmail + Form = 邮件不工作
【发布时间】:2012-07-19 16:43:56
【问题描述】:

我无法在我的 Google 应用程序中接收任何邮件。

相关代码为:

ma​​in.py

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

# Sets the "inicio.html" website as the default page
class IndexHandler(webapp.RequestHandler):
    def get(self):
        path='inicio.html'
        if self.request.url.endswith('/'):
            path = '%sinicio.html'%self.request.url

        self.redirect(path)

    def post(self):have the following 
        self.get()

# Sends an email with the fields of the form
class OnSendFormHandler(webapp.RequestHandler):
  def post(self):
      cf_name=self.request.get('cf_name')
      cf_email=self.request.get('cf_email')
      cf_subject=self.request.get('cf_subject')
      cf_body=self.request.get('cf_message')

      message = mail.EmailMessage(sender="GAE Account <validAccount@appspot.gserviceaccount.com>",
                                  to = "personalAccount <existentAccount@gmail.com>",
                                  subject = cf_subject,
                                  body = cf_body)
      message.send()

application = webapp.WSGIApplication([('/.*', IndexHandler),
                                      ('/on_send_form', OnSendFormHandler)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

请注意,表单“/on_send_form”有一个处理程序。

相关的html表单:

       <form action="/on_send_form" method="post" id="contacts-form">
        <fieldset>
          <div class="grid3 first">
            <label title="Escriba su nombre y apellidos">Nombre:<br />
              <input type="text" name="cf_name" value=""/>
            </label>
            <label title="Escriba la dirección de correo electrónico donde quiere que le enviemos la respuesta a su consulta">E-mail:<br />
              <input type="email" name="cf_email" value=""/>
            </label>
            <label title="Escriba la razón principal de su mensaje">Asunto:<br />
              <input type="text" name="cf_subject" value="" title="Escriba la razón principal de su mensaje"/>
            </label>
          </div>
          <div class="grid5">Mensaje:<br />
            <textarea name="cf_message" title="Escriba su consulta con detalle. Le responderemos a la dirección de correo electrónico indicada en un plazo máximo de 24 horas"></textarea>
            <div class="alignright">
              <a href="#" class="alt" onClick="document.getElementById('contacts-form').reset()">Limpiar Campos</a> &nbsp; &nbsp; &nbsp;<a href="#" class="alt" onClick="document.getElementById('contacts-form').submit()">Enviar</a>
            </div>
          </div>
        </fieldset>
      </form>

表单和处理程序都使用 POST 方法。我使用该选项部署 GAE 应用程序。 --enable_sendmail

GAE 中的日志显示一切正常。

我阅读了文档,但我不知道我错过了。

提前谢谢你, D转换器

【问题讨论】:

  • 您无法使用--enable_sendmail 部署应用程序。那是 dev_appserver.py 的 SDK 标志。你到底在做什么?本地部署还是实际部署?
  • 真正部署在 GAE 上。那么我应该在哪里添加标志?我是否需要在 google 应用程序仪表板的某处指定我要使用邮件?
  • 你不需要做任何特别的事情;邮件 API 始终在生产中启用。您不必在开发服务器中明确打开它,因为它是用于测试的,而且 95% 的时间您都不想实际发送消息。
  • 不这么认为。据我所知,它默认启用。你收到任何错误信息吗?如果不尝试使用日志记录模块自己记录。
  • 尝试一件事。更改to = "personalAccount &lt;existentAccount@gmail.com&gt;" --> to = "existentAccount@gmail.com" 看看是否可行。

标签: python google-app-engine sendmail


【解决方案1】:

WSGIApplication 构造函数中的处理程序顺序错误;它们按照给定的顺序进行检查,'/.*' 匹配所有 URL,因此永远不会检查 '/on_send_form' on。把包罗万象的表达式放在最后。

【讨论】:

  • 这似乎是问题的原因。我更改了顺序,它似乎工作,除了我现在发现的以下问题:文件“....main.py”,第 32 行,在 post message.send() 文件“/base/python_runtime/python_lib/版本/1/google/appengine/api/mail.py”,第 900 行,在发送中引发 ERROR_MAP[e.application_error](e.error_detail) InvalidSenderError: Unauthorized sender
  • 好吧,它有效!我没有使用我的电子邮件地址。鉴于上面的 main.py,我现在唯一的问题是在发送邮件后(按下提交按钮),我看到一个白页,没有内容。 URL 显示“...appspot.com/on_send_form”。如何返回包含表单的页面?谢谢你们。
  • @Dconversor:将self.redirect('/') 添加到处理程序的末尾?
  • 非常感谢。现在一切都很完美。我也做了一些小的改动。我将为其他有类似问题的人发布运行示例的链接。再次感谢 Woobie 并感谢所有其他人的支持。 DConversor
  • 我重新提出问题,是否可以在发送邮件和重定向后显示确认消息?我只想显示一个弹出窗口。我有一个显示确认弹出的表单的 php 版本,但现在使用 python 我不知道如何只显示一个弹出窗口而没有一个全新的页面。谢谢
猜你喜欢
  • 2017-06-26
  • 2018-12-19
  • 2017-11-05
  • 2014-08-04
  • 2010-09-09
  • 2016-09-18
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
相关资源
最近更新 更多