2012-12-24 18 views
1

我正在使用Underscore + Backbone構建網站。 基本上我想知道是否可以從聯繫人表單發送電子郵件。如何使用Google Python發送骨幹應用程序的電子郵件AppEngine

這是我的骨幹型號:

class ContactModel extends Backbone.Model 

    defaults : 
     message : 'Default message' 

    validate : (attrs_) -> 

     # Validation Logique 

    sync : (method, model) -> 

     xhr = $.ajax 
       dataType: "json" 
       type: "POST" 
       url: # HERE I WANT TO SEND DATA TO GOOGLE APPENGINE 
       data: model.toJSON() 

       success : (jqXHR, textStatus) => 

        console.log 'Success', 'jqXHR_ :', jqXHR, 'textStatus_ :', textStatus 

       error : (jqXHR_, textStatus_, errorThrown_) -> 

        console.log 'Success', 'jqXHR_ :', jqXHR_, 'textStatus_ :', textStatus_, 'errorThrown_ :', errorThrown_ 

我的問題是:是否有可能檢索JSON從我的模型送到我的應用程序引擎,以模型的消息attribut發送給使用python我的電子郵件地址

+0

是。只需創建一個POST hander,獲取request.body並使用json將它變成可以在python中使用的東西,然後發送電子郵件。 –

+0

@Paul C請問您可以發表評論作爲答案,這樣可以關閉這個問題嗎? – machineghost

回答

0

最後我整理一下這種情況用下面的代碼:

import os 
import webapp2 
import logging 
import json 
from google.appengine.api import mail 

class MainPage(webapp2.RequestHandler): 

    def get(self): 

     #If request comes from the App 

     if self.request.referer == 'Your request.referer' : 

      message = self.request.get('message') 

      #If there is no message or message is empty 

      if not message and len(message) == 0: 

       self.response.headers.add_header('content-type', 'text/plain', charset='utf-8') 

       self.response.out.write('An empty message cannot be submitted') 

       return 

      #Print message 

      logging.info('Message : ' + message) 

      #Set email properties 

      user_address = 'user_address' 
      sender_address = 'sender_address' 
      subject = 'Subject' 
      body = message 

      #Send Email 

      mail.send_mail(sender_address, user_address, subject, body) 


     #If request comes from unknow sources 

     else : 

      self.response.headers.add_header('content-type', 'text/plain', charset='utf-8') 

      self.response.out.write('This operation is not allowed') 

      return 

app = webapp2.WSGIApplication([('/', MainPage)]) 
2

是的。只需創建一個POST hander,獲取request.body並使用json將它變成可以在python中使用的東西,然後發送電子郵件。

Getting Started With Forms

class Guestbook(webapp.RequestHandler): 
def post(self): 
    data = self.response.body 
    jdata = json.loads(data) 
    #send email with data in jdata 
相關問題