Python Flask that fills out a form and sends an email via SES

This commit is contained in:
paradizelost 2024-02-27 23:42:41 -06:00
commit b4beb3df46
3 changed files with 84 additions and 0 deletions

47
flasktest1.py Normal file
View File

@ -0,0 +1,47 @@
from flask import Flask, render_template, request
import boto3
import logging
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form['name']
phone = request.form['phone']
address = request.form['address']
comments = request.form['comments']
#print(name,phone,address,comments)
message = f"Name:{name}\nPhone:{phone}\nAddress:{address}\nComments:{comments}"
subject=f"Message from {name} CallBack: {phone}"
sendemail(message,subject)
return render_template('greet.html', name=name, phone=phone, address=address, comments=comments)
return render_template('form.html')
if __name__ == '__main__':
app.run(debug=True)
def sendemail(body,subject):
logging.info("Sending Email")
boto3.setup_default_session(profile_name='default')
sesclient=boto3.client('ses',region_name="us-east-2")
recipients=['dan@hamik.net']
CHARSET="UTF-8"
response = sesclient.send_email(
Destination={
"ToAddresses":recipients
},
Message= {
"Body": {
"Html": {
"Charset": CHARSET,
"Data": body
}
},
"Subject": {
"Charset": CHARSET,
"Data": subject
},
},
Source='noreply@hamik.net'
)

22
form.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Web Form</title>
</head>
<body>
<h2>Enter your information:</h2>
<form method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br>
<label for="phone">Phone Number:</label><br>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" placeholder="123-456-7890" required><br>
<label for="address">Address:</label><br>
<input type="text" id="address" name="address" required><br>
<label for="comments">Comments:</label><br>
<textarea id="comments" name="comments" rows="4" cols="50"></textarea><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

15
greet.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greeting</title>
</head>
<body>
<h2>Hello, {{ name }}!</h2>
<p>Your phone number is: {{ phone }}</p>
<p>Your address is: {{ address }}</p>
<p>Comments:</p>
<p>{{ comments }}</p>
</body>
</html>