ContactBridge

Log | Files | Refs | README

commit 490b840e0ffd284cc3bfb604063e978db9b02bec
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Tue, 31 Oct 2023 18:23:04 +0100

Initial commit.

Diffstat:
A.idea/.gitignore | 3+++
A.idea/.name | 2++
A.idea/PortfolioBridge.iml | 11+++++++++++
A.idea/inspectionProfiles/profiles_settings.xml | 7+++++++
A.idea/misc.xml | 8++++++++
A.idea/modules.xml | 9+++++++++
AMessageTagging/__init__.py | 0
AWebAPI/__init__.py | 31+++++++++++++++++++++++++++++++
AWebAPI/mailer.py | 26++++++++++++++++++++++++++
AWebInterface/__init__.py | 0
Amain.py | 15+++++++++++++++
11 files changed, 112 insertions(+), 0 deletions(-)

diff --git a/.idea/.gitignore b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name @@ -0,0 +1 @@ +PortfolioBridge +\ No newline at end of file diff --git a/.idea/PortfolioBridge.iml b/.idea/PortfolioBridge.iml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$"> + <excludeFolder url="file://$MODULE_DIR$/venv" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> +\ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ +<component name="InspectionProjectProfileManager"> + <settings> + <option name="USE_PROJECT_PROFILE" value="false" /> + <version value="1.0" /> + </settings> +</component> +\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (pythonProject)" project-jdk-type="Python SDK" /> + <component name="PyCharmProfessionalAdvertiser"> + <option name="shown" value="true" /> + </component> +</project> +\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/PortfolioBridge.iml" filepath="$PROJECT_DIR$/.idea/PortfolioBridge.iml" /> + </modules> + </component> +</project> +\ No newline at end of file diff --git a/MessageTagging/__init__.py b/MessageTagging/__init__.py diff --git a/WebAPI/__init__.py b/WebAPI/__init__.py @@ -0,0 +1,30 @@ +from flask import Flask +from flask_restx import Resource, Api, fields + +from WebAPI.mailer import * + +app = Flask(__name__) +api = Api(app) + +contact_fields = api.model('Contact', { + 'email': fields.String(required=True), + 'name': fields.String(required=True), + 'message_content': fields.String(required=True) +}) + +@api.route('/contact') +class contact(Resource): + @api.expect(contact_fields) + def post(self): + data = api.payload + email = data['email'] + name = data['name'] + message_content = data['message_content'] + + formatted_subject = format_subject(email) + formatted_message = format_message(name, email, message_content) + + send_email(formatted_subject, formatted_message) + +if __name__ == '__main__': + app.run(debug=True) +\ No newline at end of file diff --git a/WebAPI/mailer.py b/WebAPI/mailer.py @@ -0,0 +1,25 @@ +import smtplib +import json + + +def send_email(subject, message): + with open('secrets.json', 'r') as f: + credentials = json.load(f) + with open('settings.json', 'r') as f: + settings = json.load(f) + + from_email = credentials['email'] + password = credentials['password'] + to_email = settings['recipient_email'] + + with smtplib.SMTP(credentials['smtp_server'], 587) as server: + server.starttls() + server.login(from_email, password) + server.sendmail(from_email, to_email, f"Subject: {subject}\n\n{message}") + + +def format_message(name, email, content): + return f"MESSAGE RECEIVED FROM: {name}\nWITH EMAIL: {email}\nCONTENT:\n{content}" + +def format_subject(email): + return f"Contact from: {email}" +\ No newline at end of file diff --git a/WebInterface/__init__.py b/WebInterface/__init__.py diff --git a/main.py b/main.py @@ -0,0 +1,15 @@ +from flask import Flask +from flask_restx import Resource, Api + +app = Flask(__name__) +api = Api(app) + + +@api.route('/hello') +class HelloWorld(Resource): + def get(self): + return {'hello': 'world'} + + +if __name__ == '__main__': + app.run(debug=True)