I'm trying to implement a subscribe button on my website that will send whatever the user writes to one of my mails. I'm trying to write a Python script that would do that. My website is static and hosted on app engine.
My form
<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>
My python script
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="<x@x.com>",
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")
Two questions: -Any chance it will work ?
-How do I serve the script with App Engine. What should I do with my app.yaml file ? When I try to use the button it just opens the script as plain text in a new window
Thank you!
Add these lines to your app.yaml:
handlers:
- url: /subscribe/.*
script: subscribe.app
libraries:
- name: webapp2
version: "2.5.2"
Then rename your Python script to subscribe.py and change its contents to this:
#!/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="<x@x.com>",
subject='Subscribe',
body=email)
self.response.out.write("Sent mail")
app = webapp2.WSGIApplication([
('/subscribe/form', Form),
('/subscribe/send_mail', SendMail),
], debug=True)
That should get you started. I hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.