From cffddf97fe19aeb52d64633d5b47a4ccc978f8c1 Mon Sep 17 00:00:00 2001 From: Uthman Fatih Date: Sun, 19 Oct 2025 21:42:56 +0100 Subject: [PATCH] Added env error handling --- services/mail.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/services/mail.py b/services/mail.py index 84957dd..5fd8538 100644 --- a/services/mail.py +++ b/services/mail.py @@ -13,7 +13,12 @@ from email import message_from_bytes from dotenv import load_dotenv from os import getenv -from os.path import dirname, join +from os.path import dirname, join, exists + +# Exit if .env does not exist +if not exists(join(dirname(__file__), "../.env")): + print("Please create a .env file in the root directory.") + exit(1) # Load environment variables from .env file dotenv_path = join(dirname(__file__), "../.env") @@ -21,16 +26,24 @@ load_dotenv(dotenv_path) # SMTP server settings SMTP_SERVER = getenv("SMTP_SERVER") -SMTP_PORT = int(getenv("SMTP_PORT")) +SMTP_PORT = getenv("SMTP_PORT") SMTP_USERNAME = getenv("SMTP_USERNAME") SMTP_PASSWORD = getenv("SMTP_PASSWORD") # IMAP server settings IMAP_SERVER = getenv("IMAP_SERVER") -IMAP_PORT = int(getenv("IMAP_PORT")) +IMAP_PORT = getenv("IMAP_PORT") IMAP_USERNAME = getenv("IMAP_USERNAME") IMAP_PASSWORD = getenv("IMAP_PASSWORD") +# Check if all environment variables are set +if not SMTP_SERVER or not SMTP_PORT or not SMTP_USERNAME or not SMTP_PASSWORD or not IMAP_SERVER or not IMAP_PORT or not IMAP_USERNAME or not IMAP_PASSWORD: + print("Please set all environment variables in .env file.") + exit(1) + +SMTP_PORT = int(SMTP_PORT) +IMAP_PORT = int(IMAP_PORT) + def send_email(receiver_emails, subject, body): sender_email = SMTP_USERNAME