From b4beb3df46e9e4253f112423b3cb7738b612fc51 Mon Sep 17 00:00:00 2001 From: paradizelost Date: Tue, 27 Feb 2024 23:42:41 -0600 Subject: [PATCH] Python Flask that fills out a form and sends an email via SES --- flasktest1.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ form.html | 22 ++++++++++++++++++++++ greet.html | 15 +++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 flasktest1.py create mode 100644 form.html create mode 100644 greet.html diff --git a/flasktest1.py b/flasktest1.py new file mode 100644 index 0000000..fd565dd --- /dev/null +++ b/flasktest1.py @@ -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' + ) \ No newline at end of file diff --git a/form.html b/form.html new file mode 100644 index 0000000..b138dcd --- /dev/null +++ b/form.html @@ -0,0 +1,22 @@ + + + + + + Simple Web Form + + +

Enter your information:

+
+
+
+
+
+
+
+
+

+ +
+ + diff --git a/greet.html b/greet.html new file mode 100644 index 0000000..531d230 --- /dev/null +++ b/greet.html @@ -0,0 +1,15 @@ + + + + + + Greeting + + +

Hello, {{ name }}!

+

Your phone number is: {{ phone }}

+

Your address is: {{ address }}

+

Comments:

+

{{ comments }}

+ +