2016-05-26 8 views
0

自分のメールにユーザーが書き込んだものを送信するウェブサイトにサブスクライブボタンを実装しようとしています。 私はこれを行うPythonスクリプトを書こうとしています。 私のウェブサイトは静的で、アプリエンジンでホストされています。Google App EngineでPythonを使用してフォームコンテンツを送信

マイフォーム

   <form method="post" action="assets/python/mail.py" class="container 50%"> 
       <input type="email" name="email" id="email" placeholder="Ton adresse email" />< 
       <input type="submit" value="S'inscrire" class="fit special" /> 
       </form> 

私のPythonスクリプト

from google.appengine.api import mail 
from google.appengine.api import app_identity 
import cgi 


def send_approved_mail(sender_address, mail, type): 
    # [START send_mail] 
    mail.send_mail(sender=sender_address, 
        to="<[email protected]>", 
        subject=type, 
        body=mail) 
    # [END send_mail] 
form = cgi.FieldStorage() 
if form.has_key("email"): 
    mail = form["email"].value 
    send_approved_mail('{}@appspot.gserviceaccount.com'.format(app_identity.get_application_id()), mail, "client") 

つの質問:それは動作します -anyチャンス?

- App Engineでスクリプトをどのように提供しますか? app.yamlファイルはどうすればよいですか?ボタンを使用しようとすると、新しいウィンドウでプレーンテキストとしてスクリプトが開かれます。

ありがとうございました!

答えて

0

はをapp.yamlに次の行を追加します。

handlers: 
- url: /subscribe/.* 
    script: subscribe.app 

libraries: 
- name: webapp2 
    version: "2.5.2" 

次にこれにその内容をsubscribe.pyし、変更するには、Pythonスクリプトの名前を変更します。

#!/usr/bin/env python 

import cgi 
import webapp2 
from google.appengine.api import mail 
from google.appengine.api import app_identity 


class Form(webapp2.RequestHandler): 
    def get(self): 
    self.response.out.write(''' 
<form method="post" action="/subscribe/send_mail" class="container 50%"> 
<input type="text" name="email" id="email" placeholder="Ton adresse email" /> 
<input type="submit" value="S'inscrire" class="fit special" /> 
</form> 
''') 

class SendMail(webapp2.RequestHandler): 
    def post(self): 
    email = self.request.get('email') 
    sender = '{}@appspot.gserviceaccount.com'.format(app_identity.get_application_id()) 
    mail.send_mail(sender=sender, 
        to="<[email protected]>", 
        subject='Subscribe', 
        body=email) 
    self.response.out.write("Sent mail") 


app = webapp2.WSGIApplication([ 
    ('/subscribe/form', Form), 
    ('/subscribe/send_mail', SendMail), 
], debug=True) 

あなたが始める必要があること。私はそれが助けて欲しい!

+0

お寄せいただきありがとうございます。 webapp2の代わりにFlaskを使用していました(私はFlaskをよく知っていました) – Hij4ck

関連する問題