From e384976686aeb95c93129197d900820176cc5d02 Mon Sep 17 00:00:00 2001 From: white <122345776@umail.ucc.ie> Date: Wed, 29 Jan 2025 14:12:01 +0000 Subject: [PATCH] FEAT: Added email bot (not connected) UPDATE: fixed recommended categories --- web_server/blueprints/email.py | 51 ++++++++++++++++++++++++++++++++ web_server/blueprints/streams.py | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 web_server/blueprints/email.py diff --git a/web_server/blueprints/email.py b/web_server/blueprints/email.py new file mode 100644 index 0000000..f581dd3 --- /dev/null +++ b/web_server/blueprints/email.py @@ -0,0 +1,51 @@ +from flask import Blueprint, session +from database.db_context import get_db +import smtplib +from email.mime.text import MIMEText +from os import getenv +from random import randrange + +email_bp = Blueprint("email", __name__) + +@email_bp.route("/send_email", methods=["POST"]) +def send_email() -> None: + """ + Send a verification email to the user. + """ + # Setup the sender email details + SMTP_SERVER = "smtp.gmail.com" + SMTP_PORT = 587 + SMTP_EMAIL = "" + SMTP_PASSWORD = getenv() + + # Get the users email address + db = get_db() + db.create_connection() + user_email = db.fetchone(""" + SELECT email + FROM users + WHERE username = ?; + """, (session.get("username"),)) + + + # Setup up the receiver details + login_code = randrange(100000, 1000000) + body = f"Here is your login code: {login_code}" # Make this better + msg = MIMEText(body) + msg["Subject"] = "Your Gander Login Code" + msg["From"] = SMTP_EMAIL + msg["To"] = user_email + + # Send the email using smtplib + with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp: + try: + smtp.starttls() # TLS handshake to start the connection + smtp.login(SMTP_EMAIL, SMTP_PASSWORD) + smtp.ehlo() + smtp.send_message(msg) + + except TimeoutError: + print("Server timed out") + + except Exception as e: + print("Error: ", e) \ No newline at end of file diff --git a/web_server/blueprints/streams.py b/web_server/blueprints/streams.py index b8b9829..9c1cecc 100644 --- a/web_server/blueprints/streams.py +++ b/web_server/blueprints/streams.py @@ -46,7 +46,7 @@ def get_categories() -> list[dict]: """ category_data = most_popular_category() - streams = recommendations_based_on_category(category_data[0]) + streams = recommendations_based_on_category(category_data["category_id"]) return jsonify(streams) @login_required