Python send mail on button click Google App Engine - python

Frustration: I have searched most of the websites and still was not able to find any answer.
Info: I don't know anything about Python.
What I want to code: I want to send a mail using Python on button click of an HTML web page. It should not open the user's email client, like 'mailto' does in html, but directly send me the mail. The web application is hosted using Google App Engine (url is like www.example.appspot.com). I want to also send the values in text box and radio group to be sent along to the recipients email address.
HTML form:
<form method="post" id="rsvpForm" action="">
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<br/>
<div>
<div class="12u">
<span>Radio grp 1</span>
</div>
</div>
<div class="row half">
<div class="12u">
<input type="radio" name="attendance" value="Options">Options<br>
</div>
</div>
<br/>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message or wishes" rows="3">
</textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<ul class="actions">
<li><input type="submit" class="style1" value="Send" id="sendMessage"/>
</li>
<li><input type="reset" class="style2" value="Reset" /></li>
</ul>
</div>
</div>
Reference Used: I used this site to host the website
http://www.labnol.org/internet/host-website-on-google-app-engine/18801/
Please help me. I don't want to use PHP

Send an email from GAE Python is very easy, but you post only the HTML code, have you a python handler in place to do that? If not I suggest to learn and test all these tutorial provided by google: https://developers.google.com/appengine/docs/python/gettingstartedpython27/introduction
This is a sample to send an email form GAE python: https://developers.google.com/appengine/docs/python/mail/?hl=fr#Python_Sending_mail_in_Python

Related

HTML5 - Send message button not returning my function?

I'm brand new to coding and am following the Udemy Zero to Mastery course. I'm currently having trouble getting my HTML5 to process a function I've made in Python. I want to return my function, submit_form, when I click on 'send message'. Here's the HTML5:
<section id="three">
<h2>Get In Touch</h2>
<p>This is the contact section.</p>
<div class="row">
<div class="col-8 col-12-small">
<form method="post" action="index.html">
<div class="row gtr-uniform gtr-50">
<div class="col-6 col-12-xsmall"><input type="text" name="name" id="name"
placeholder="Name" /></div>
<div class="col-6 col-12-xsmall"><input type="email" name="email" id="email"
placeholder="Email" /></div>
<div class="col-12"><textarea name="message" id="message" placeholder="Message"
rows="4"></textarea></div>
</div>
</form>
<ul class="actions">
<!-- <button type="submit" class="btn btn-default btn-lg">Send Message</button> -->
<li><input type="button" value="Send Message" /></li>
</form>
</ul>
</div>
And here is my python function:
#app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
return 'form submitted!'
I'm having a lot of trouble understanding how to make the send message button return my function. Any help is much appreciated, thank you!

Invalid Request Origin after Python post request

I've been trying to write a program which gets my school timetable which is locked behind a login screen, however when I run my python script I am greeted by:
Sorry, we are unable to complete this request
An error occured while submitting the form:
Invalid Request Origin
This is the code I've written so far:
import requests
login_url = 'https://WEBSITE/login?page=%2Fhomepage'
timetable_url = 'https://WEBSITE/timetable'
payload = {
'username': 'USERNAME',
'password': 'PASSWORD'
}
with requests.Session() as session:
post = session.post(login_url, data=payload, verify = True)
r = session.get(timetable_url)
print (post.text)
And this is the html from the login page:
<div id="content">
<form id="login" method="post" action="/login?page=%2Flogin%2F">
<input type="hidden" name="page" value="/login/" />
<div class="row collapse">
<div class="small-12 column">
<div class="logo">
<img src="/images/logo.php?logo=skin_logo_login&size=normal" alt="Schoolbox" />
</div>
</div>
<div class="small-12 column">
<p id="error-msg" class="alert-box alert"></p>
</div>
<div class="small-12 column">
<input type="text" name="username" id="username" placeholder="Username" />
<input type="password" name="password" id="password" placeholder="Password" />
</div>
</div>
<div class="row collapse">
<div class="small-12 column">
<p>Forgotten your password?</p>
</div>
<div class="small-12 column">
<input id="rememberme" type="checkbox" name="rememberme" value="1" />
<label for="rememberme">Remember me</label>
</div>
<div class="small-12 column login-links">
<input type="submit" name="Submit" value="Login" alt="Login" />
</div>
</div>
</form>
</div>
How exactly would I be able to solve this error, or would there be any other more viable solutions? Thank you.
This error Invalid Request Origin means that your origin does not match what is restricted by the server. You could try adding the Origin: header specifying your timetable_url, but it is likely that the page can only be called by another page on the server.

python login through web portal

Having issues using python to login to web portal. I've tried using suggestions from other posts
How can I login to a website with Python?
I've tried using twill (which failed to install because I'm running Python 3.6) - if anyone has suggestions how I can install it please advise
I've also tried using:
import requests
url = 'http://example.com/userinfo.php'
values = {'username': 'user',
'password': 'pass'}
r = requests.post(url, data=values)
print(r.content)
with no luck.
The login page has the following html
<form id="form" method="post" action="/" style="padding-left:20px;" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="username">User Name:</label>
<div class="controls">
<input id="username" type="text" placeholder="User Name" autocomplete="off" autocorrect="off" autocapitalize="off">
</div>
</div>
<div class="control-group">
<label class="control-label" for="ba_password2">Password:</label>
<div class="controls">
<input id="ba_password2" type="password" placeholder="Password">
<input type="hidden" id="ba_username" name="ba_username">
<input type="hidden" name="ba_password">
<input type="hidden" name="ba_seed" value="614377365">
<input type="hidden" name="ba_salt" value="dgvwghgwuoyixgsrdhblkgigmtrhivbx">
<input type="hidden" name="ba_seedkey" value="-1647247092">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="rememberMe"> Remember me on this computer
</label>
<button id="ba_loginbut" type="submit" class="btn">Login</button>
</div>
</div>
<div class="msgHolder">
<div id="login-error-box" class="errormsg" style="display:none;">
<img src="/images/err.png"/>
<div id="login-error-msg"></div>
</div>
<div id="capslock" class="errormsg" style="display:none;">
<img src="/images/warn.png"/>
Caps lock is on.
</div>
</div>
</form>
I hope someone can help. Thanks
Consider using libraries like webbot which even handle dynamically changing class and id for the webpage elements.
With a Python code must looks like this:
#Open Firefox browser
driver = webdriver.Firefox()
#Enter email
elem = driver.find_element_by_id("username")
elem.clear()
elem.send_keys("email")
#Enter password
elem = driver.find_element_by_id("ba_password2")
elem.clear()
elem.send_keys("pass")
#Click button
elem = driver.find_element_by_id("ba_loginbut")
elem.click()

Send Text from HTML to Python

I am running an html page on apache2:
http://localhost/test.html
I want to send the username to a python script check.py, How can I do that?
html:
<div id="wrapper">
<form name="login-form" class="login-form" action="check.py" method="post" onsubmit="validate();return false;">
<div class="header">
<h1>Login Form</h1>
<span>Fill out the form below to login to my super awesome imaginary control panel.</span>
</div>
<div class="content">
<input id="Uname" name="username" type="text" class="input username" placeholder="Username" />
<div class="user-icon"></div>
<input id="Pname"name="password" type="password" class="input password" placeholder="Password" />
<div class="pass-icon"></div>
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button"/>
<input type="submit" name="submit" value="Register" class="register" />
</div>
</form>
</div>
<div class="gradient"></div>
</body>
</html>
Well you are running a static page, their is no way you can pass the page data to python file this way.
What you need is to learn server side programming.
In python Django and Flask are the popular framework for server side programming.
Below is a question related to flask. that will suits your need
Sending data from HTML form to a Python script in Flask

How do I use Python to Login to a website using urllib2 and then open a browser page to the logged in site?

I saw a similar question asking for help logging into reddit here.
The site I want to login seems more complex. The form from the HTML looks like this:
<div class="bottom-wrapper">
<div class="sidebar-container">
<div id="logged-in-user">
<div class="ajax-loading"></div>
<div class="panel-pane pane-type1 anonymous-content" id="pane-login-block">
<h2 class="pane-title">Login</h2>
<div id="login-section" class="pane-content">
<form method="post" action="https://www.fancywebsite.com/php/login.php">
<input type="hidden" name="destination" value="">
<input type="hidden" value="user_login" name="form_id">
<input type="hidden" value="2800" name="affid">
<input type="hidden" value="0" name="blocklogin">
<input type="hidden" value="1" name="wager">
<input id="edit-redirect" type="hidden" value="http://www.fancywebsite.com/main" name="redirect">
<ul class="field-set">
<li>
<label for="username">Username:</label>
<input type="text" name="acct" id="username" class="text-box" maxlength="100" size="20">
</li>
<li>
<label for="password">Password:</label>
<input type="password" name="pin" id="password" class="text-box" maxlength="16" size="20">
</li>
<li>
<span id="reset-login-link">forgot your login information?</span>
<input type="submit" class="button" value="Login" id="Login" name="Login">
</li>
</ul>
</form>
I want Python to go to that login page and enter my credentials and then open a browser to the logged in page. Any help would be greatly appreciated. I tried to adapt the method from the reddit example work, without luck. I don't want to use mechanize. Thanks in advance.
You can use either requests and urllib2 to login or you can use mechanize to simulate the browser, which is a better way I think.
Here is link you can refer to:
requests or mechanize

Categories

Resources