2012-07-19 63 views
0

我无法在我的谷歌应用程序中收到任何邮件。GAE + Python + Sendmail +表单=邮件无法正常工作

相关代码:

main.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 <[email protected]>", 
            to = "personalAccount <[email protected]>", 
            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中的日志表示一切正常。

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

谢谢你在前进, DConversor

+0

你不能用'--enable_sendmail'部署一个应用程序。这是dev_appserver.py的SDK标志。你究竟在做什么?本地还是真的部署? – aschmid00 2012-07-19 16:54:31

+0

真正部署在GAE上。那么我在哪里添加标志呢?我是否需要指定我想要在谷歌应用程序仪表板中的某处使用邮件? – Dconversor 2012-07-19 17:06:58

+0

你不需要做任何特别的事情;邮件API始终在生产中启用。您不必在开发服务器上明确地打开它,因为它旨在用于测试,并且95%的时间您不想实际发送消息。 – geoffspear 2012-07-19 17:16:32

回答

2

你处理你的WSGIApplication构造函数是在错误的顺序;他们按照给定的顺序进行检查,并且'/.*'与所有URL匹配,因此'/on_send_form'未被检查。把最后的表情放在最后。

+0

这似乎是问题的原因。我改变了顺序,它似乎工作,除了我现在发现的以下问题:文件“.... main.py”,第32行,后 message.send() 文件“/ base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py“,第900行,发送 raise ERROR_MAP [e.application_error](e.error_detail) InvalidSenderError:未经授权的发件人 – Dconversor 2012-07-19 17:30:50

+0

Okey,它的工作原理!我没有使用我的电子邮件地址。考虑到上面的main.py,现在唯一的问题是,在发送邮件(按提交按钮)后,我看到一个白色页面,没有内容。该网址显示'... appspot.com/on_send_form'。我怎样才能返回到包含表单的页面?感谢你们。 – Dconversor 2012-07-19 17:43:41

+0

@Dconversor:将'self.redirect('/')'添加到处理程序的末尾? – geoffspear 2012-07-19 17:46:44