如何用谷歌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从我的模型发送到我的应用程序引擎,以便发送模型的消息属性到我的电子邮件地址使用python

是。只需创建一个POST处理程序,获取请求。然后使用json将其转换为可以在python中使用的内容,然后发送电子邮件。

表单入门

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

最后我用下面的代码来整理这种情况:

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)])

相关内容

最新更新