From b2754db79360cdf22c15a7f2f1cc310a6c360f9e Mon Sep 17 00:00:00 2001 From: Blake Ridgway Date: Tue, 11 Feb 2025 21:44:13 -0600 Subject: [PATCH] (feat): Added email sending and remove_emails function --- server.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 6532065..2e2b4b8 100644 --- a/server.py +++ b/server.py @@ -1,11 +1,41 @@ -# noinspection PyPackageRequirements +import os +import smtplib +from email.mime.text import MIMEText from flask import Flask, render_template, request, jsonify +from database import init_db, add_email, remove_email +from dotenv import load_dotenv -from database import init_db, add_email - +load_dotenv() app = Flask(__name__) init_db() +def send_confirmation_email(email): + SMTP_SERVER = os.getenv('SMTP_SERVER') + SMTP_PORT = int(os.getenv('SMTP_PORT', 465)) + SMTP_USER = os.getenv('SMTP_USER') + SMTP_PASSWORD = os.getenv('SMTP_PASSWORD') + + # Create the message for the + unsubscribe_link = f"{request.url_root}unsubscribe?email={email}" + subject = 'Thanks for subscribing!' + body = ("Thanks for subscribing!\n\n" + "We're excited to share our journey with you.\n\n" + f"If you ever wish to unsubscribe, please click here." + ) + + msg = MIMEText(body, 'html', 'utf-8') + msg['Subject'] = subject + msg['From'] = SMTP_USER + msg['To'] = email + + try: + server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, timeout=10) + server.login(SMTP_USER, SMTP_PASSWORD) + server.sendmail(SMTP_USER, email, msg.as_string()) + server.quit() + except Exception as e: + print(f"Failed to send email to {email}: {e}") + @app.route("/") def index(): return render_template("index.html") @@ -18,9 +48,21 @@ def subscribe(): return jsonify({"error": "No email provided"}), 400 if add_email(email): + send_confirmation_email(email) return jsonify({"message": "Email has been added"}), 201 else: return jsonify({"error": "Email already exists"}), 400 +@app.route("/unsubscribe", methods=["GET"]) +def unsubscribe(): + email = request.args.get("email") + if not email: + return "No email specified.", 400 + + if remove_email(email): + return f"The email {email} has been unsubscribed.", 200 + else: + return f"Email {email} was not found or has already been unsubscribed.", 400 + if __name__ == "__main__": app.run(debug=True)