2012-07-19 60 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